天天看點

Spring基礎(八):注解方式建立對象IOC

Spring基礎(八):注解方式建立對象IOC

文章目錄

​​注解方式建立對象IOC​​

​​一、注解方式建立對象IOC​​

​​1、在applicationContext.xml中配置開啟注解掃描​​

​​2、在類上添加注解,讓spring容器給我們建立bean執行個體并存儲于容器中​​

​​3、測試代碼​​

​​4、元件掃描配置注解識别​​

​​二、注解方式依賴注入DI​​

​​三、完全使用注解​​

注解方式建立對象IOC

一、注解方式建立對象IOC

導入依賴 aop

@Component    放在類上,用于标記,告訴spring目前類需要由容器執行個體化bean并放入容器中

該注解有三個子注解

@Controller   用于執行個體化controller層bean

@Service        用于執行個體化service層bean

@Repository  用于執行個體化持久層bean

當不确定是哪一層,就用Component

這幾個注解互相混用其實也可以,但是不推薦

1、在applicationContext.xml中配置開啟注解掃描

<?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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
">
    <!--添加注解掃描,掃描指定的包,将包中的所有有注解的類執行個體化
    base-package 後面放要掃描的包
    如果有多個包需要掃描,可以使用逗号隔開  com.lanson.bean,com.lanson.service
    或者可以寫上一層包路徑  com.lanson
    可以通過注解指定bean的id@Component("user1")
    如果不指定,則id預設是 類名首字母小寫
    -->
    <context:component-scan base-package="com.lanson.bean"></context:component-scan>
</beans>      

2、在類上添加注解,讓spring容器給我們建立bean執行個體并存儲于容器中

package com.lanson.bean;
import org.springframework.stereotype.Component;
/**
 * @Author: Lansonli
 * @Description: MircoMessage:Mark_7001
 */
@Component(value = "user1")
public class User {
}      

3、測試代碼

package com.lanson.test;
import com.lanson.bean.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * @Author: Lansonli
 * @Description: MircoMessage:Mark_7001
 */
public class Test1 {
    @Test
    public void testGetBean(){
        ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = context.getBean("user1", User.class);
        System.out.println(user);
    }
}      

4、元件掃描配置注解識别

<!--
    use-default-filters="false"
    預設值為true 代表使用預設的掃描過濾器
    預設的掃描過濾器會識别并包含 @Component @Controller @Service @Repository 四個注解
    不使用預設的filter,使用我們自己的filter
-->
    <!--控制隻掃描Controller注解-->
    <context:component-scan base-package="com.msb" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!--控制不掃描Controller注解-->
    <context:component-scan base-package="com.msb" use-default-filters="true">
        <context:exclude-filter  type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>      

二、注解方式依賴注入DI

@Autowired   根據屬性資料類型自動裝配

@Qualifier      根據屬性名稱注入依賴

@Resources   可以根據類型,也可以根據名稱注入

@Value           注入普通資料類型(8+String)

項目結構如下

Spring基礎(八):注解方式建立對象IOC

applicationContext.xml中配置包掃描和讀取屬性配置檔案

Dao層接口

Spring基礎(八):注解方式建立對象IOC

實作類

Spring基礎(八):注解方式建立對象IOC
Spring基礎(八):注解方式建立對象IOC

讓容器掃描 Service層,執行個體化對象接口

Spring基礎(八):注解方式建立對象IOC

實作類

package com.lanson.service.impl;
import com.lanson.dao.UserDao;
import com.lanson.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
 * @Author: Lansonli
 * @Description: MircoMessage:Mark_7001
 */
@Service
public class UserServiceImpl implements UserService {
    /*
    *@Autowired
    * 根據類型到容器中去尋找對應的對象,找到後給目前屬性指派
    * 不需要依賴 set方法
    * 屬性類型可以是接口,會自動比對對應的實作類對象
    * @Autowired配合 @Qualifier,可以通過名稱指定注入的對象
    *
    * @Resource 如果不配置name 那麼就是根據類型注入
    * @Resource(name="userDaoImplB") 配置name,就是根據名稱注入
    *
    *
    * @Resource  是JDK中javax包的注解
    * @Autowired 和 @Qualifier 是spring中的注解
    *
    * @Value 可以個普通屬性指派
    * @Value 可以使用${}這種表達式擷取系統的變量值
    *        或者是.properties屬性配置檔案中的值
    *
    * */
    //@Autowired
    //@Qualifier("userDaoImplA")
    //@Qualifier("userDaoImplB")
    //private UserDao userDao ;
    @Resource(name="userDaoImplB")
    private UserDao userDao ;
    @Value("${username}")
    private String sname;
    @Value("boy")
    private String sgender;
    @Value("${age}")
    private Integer sage;
    @Override
    public void add() {
        System.out.println("userServiceImpl add ... ... ");
        System.out.println(sname);
        System.out.println(sgender);
        System.out.println(sage);
        userDao.add();
    }
}      

測試代碼

package com.lanson.test;
import com.lanson.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * @Author: Lansonli
 * @Description: MircoMessage:Mark_7001
 */
public class Test1 {
    @Test
    public void testGetBean(){
        ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = context.getBean("userServiceImpl", UserService.class);
        userService.add();
    }
}      

三、完全使用注解

@Test
public void testGetBean2(){
    ApplicationContext context=new AnnotationConfigApplicationContext(SpringConfig.class);
    UserServiceImpl userService = context.getBean("userServiceImpl", UserServiceImpl.class);
    userService.add();
}      
  • 📢歡迎點贊 👍 收藏 ⭐留言 📝 如有錯誤敬請指正!
  • 📢本文由 Lansonli 原創,
  • 📢停下休息的時候不要忘了别人還在奔跑,希望大家抓緊時間學習,全力奔赴更美好的生活✨