1. 程式人生 > >5.Spring MVC 自動裝配問題

5.Spring MVC 自動裝配問題

spring getbean ram ng- get wire enc system say

一.使用@controller註解,實際上也是在IOC容器中配置了,它的id是類的首字母小寫

一.使用@controller註解,實際上也是在IOC容器中配置了,它的id是類的首字母小寫

1.如果不使用註解,在IOC容器中通過配置來加載bean。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!--加載bean--> <bean id="userController"
class="com.neuedu.controller.UserController"></bean> </beans>

如果使用註解的方式,在配置文件中要掃描包

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">   <!-- 掃描包--> <context:component-scan base-package="com.neuedu"></context:component-scan> </beans>

寫一個controller層

ackage com.neuedu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.neuedu.service.UserService;

@Controllerpublic class UserController {
  public void sayHello(){
        
       System.out.println("say Hello");
    }

}

使用@controller註解,實際上也是在IOC容器中配置了,它的id是類的首字母小寫

可以寫一個Junit test case

public class TestIOC {
    private ApplicationContext ioc=new ClassPathXmlApplicationContext("applicationContext.xml");

    @Test
    public void test() {
//使用UserController類的id來調用
        Object bean = ioc.getBean("userController");
        System.out.println(bean);
    }

}

@controller也可以更改id,這個註解有一個value屬性值

比如:@Controller(value=" zhangsan")

package com.neuedu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.neuedu.service.UserService;

@Controller(vaqlue="zhangsan")
public class UserController {
  
    public void sayHello(){        
       System.out.println("say Hello");
    }
}

在Junit test case中

public class TestIOC {
    private ApplicationContext ioc=new ClassPathXmlApplicationContext("applicationContext.xml");

    @Test
    public void test() {
//使用UserController類的id來調用
        Object bean = ioc.getBean("zhangsan");
        System.out.println(bean);
    }
}

@Autowired標簽

@Controller(value="zhangsan")
public class UserController {
    @Autowired
    private UserService userService;
    public void sayHello(){        
        userService.sayHello();    }

}

Autowired標簽

  1.首先是將使用 UserService 類,

  2.如果類有沖突就使用 id,其實就屬性名userService

1]首先檢測標記了@Autowired註解的屬性的類型
[2]根據類型進行裝配
[3]如果指定類型的bean不止一個,那麽根據需要被裝配的屬性的屬性名做id的值,查找bean
[4]如果根據id值還是沒有找到bean,可以使用@Qualifier註解手動指定要裝配的bean的id.

5.Spring MVC 自動裝配問題