SpringMVC使用注解配置bean


如果不使用注解,在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層

package 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.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM