欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring IOC相關(guān)注解運(yùn)用(上篇)

 更新時間:2023年05月05日 11:39:08   作者:會洗碗的CV工程師  
這篇文章主要介紹了Spring?IOC相關(guān)注解的運(yùn)用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

前言

注解配置和xml配置對于Spring的IOC要實(shí)現(xiàn)的功能都是一樣的,只是配置的形式不一樣。
準(zhǔn)備工作:

  • 創(chuàng)建一個新的Spring項(xiàng)目。
  • 編寫pojo,dao,service類。
  • 編寫空的配置文件,如果想讓該文件支持注解,需要在bean.xml添加新的約束:
<?xml version="1.0" encoding="UTF-8" ?>
<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        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">
 
    <context:component-scan base-package="com.example"/>
    <context:property-placeholder location="db.properties"/>
 
</beans>

一、@Component

@Component可以代替bean標(biāo)簽

  • 作用:用于創(chuàng)建對象,放入Spring容器,相當(dāng)于 <bean id="" class="">
  • 位置:類上方
  • 注意:@Component 注解配置bean的默認(rèn)id是首字母小寫的類名。也可以手動設(shè)置bean的id值。
// 此時bean的id為studentDaoImpl
@Component
public class StudentDaoImpl implements StudentDao{
  public Student findById(int id) {
    // 模擬根據(jù)id查詢學(xué)生
    return new Student(1,"程序員","北京");
 }
// 此時bean的id為studentDao
@Component("studentDao")
public class StudentDaoImpl implements StudentDao{
  public Student findById(int id) {
    // 模擬根據(jù)id查詢學(xué)生
    return new Student(1,"程序員","北京");
 }
}

二、@Repository、@Service、@Controller

作用:這三個注解和@Component的作用一樣,使用它們是為了區(qū)分該類屬于什么層。
位置:

  • @Repository用于Dao層
  • @Service用于Service層
  • @Controller用于Controller層
@Repository
public class StudentDaoImpl implements StudentDao{}
@Service
public class StudentService {}

三、@Scope

作用:指定bean的創(chuàng)建策略
位置:類上方
取值:singleton prototype request session globalsession 

@Service
@Scope("singleton")
public class StudentService {}

測試一下: 

    @Test
    public void t1(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        StudentDao studentDao = (StudentDao) ac.getBean("studentDao");
        System.out.println(studentDao);
        StudentService service1 = (StudentService) ac.getBean("studentService");
        System.out.println(service1.hashCode());
        StudentService service2 = ac.getBean("studentService",StudentService.class);
        System.out.println(service2.hashCode());
    }

OK,確實(shí)可以

四、@Autowired

作用:從容器中查找符合屬性類型的對象自動注入屬性中。用于代替 <bean> 中的依賴注入配置。
位置:屬性上方、setter方法上方、構(gòu)造方法上方。
注意:@Autowired 寫在屬性上方進(jìn)行依賴注入時,可以省略setter方法。

@Component
public class StudentService {
  @Autowired
  private StudentDao studentDao;
  public Student findStudentById(int id){
    return studentDao.findById(id);
 }
}
 
// 測試方法
@Test
public void t2(){
  ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
  StudentService studentService = (StudentService) ac.getBean("studentService");
  System.out.println(studentService.findStudentById(1));
}

測試結(jié)果:

OK,也是可以的 

五、@Qualifier

作用:在按照類型注入對象的基礎(chǔ)上,再按照bean的id注入。
位置:屬性上方
注意:@Qualifier必須和@Autowired一起使用。

如下

@Component
public class StudentService {
  @Autowired
  @Qualifier("studentDaoImpl2")
  private StudentDao studentDao;
  public Student findStudentById(int id){
    return studentDao.findById(id);
 }
}

六、@Value

作用:注入String類型和基本數(shù)據(jù)類型的屬性值。
位置:屬性上方

以下說明一下用法:

1. 直接設(shè)置固定的屬性值

    @Value("1")
    private int count;
 
    @Value("hello")
    private String str;

2. 獲取配置文件中的屬性值

編寫配置文件db.properties

jdbc.username=root
jdbc.password=123456

spring核心配置文件(bean.xml)掃描配置文件

<context:property-placeholder location="db.properties"/>

注入配置文件中的屬性值

    @Value("${jdbc.username}")
    private String username;
 
    @Value("${jdbc.password}")
    private String password;

3. 測試結(jié)果

測試方法

    // 測試注解Value
    @Test
    public void t3(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        StudentService service = ac.getBean("studentService",StudentService.class);
        System.out.println(service);
    }

運(yùn)行結(jié)果

OK,應(yīng)該和上面設(shè)置的值一樣,說明可以使用,本篇就介紹到這幾個注解了,下篇會介紹完接下來的注解。 

以上就是Spring IOC相關(guān)注解運(yùn)用(上篇)的詳細(xì)內(nèi)容,更多關(guān)于Spring IOC注解的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論