目錄
@Component的使用
依賴注解的使用
非自定義Bean的注解開發
@Component的使用
基本Bean注解,主要是使用注解的方式替代原有的xml的<bean>標簽及其標簽屬性的配置,使用@Component注解替代<bean>標簽中的id以及class屬性,而對于是否延遲加載或是Bean的作用域,則是其他注解
xml配置 | 注解 | 描述 |
<bean scope=""> | @Scope | 在類上或使用了@Bean標注的方法上,標注Bean的作用范圍,取值為singleton或prototype |
<bean lazy-init=""> | @Lazy | 在類上或使用了@Bean標注的方法上,標注Bean是否延遲加載,取值為true或false |
<bean init-method=""> | @PostConstruct | 在方法上使用,標注Bean的實例化后執行的方法 |
<bean destroy-method=""> | @PreDestroy | 在方法上使用,標注Bean的銷毀前執行方法 |
下面就是基于注解的測試案例
首先需要開啟自動掃描注解功能,這個功能還是需要在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: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 https://www.springframework.org/schema/context/spring-context.xsd"><!--開啟自動掃描--><context:component-scan base-package="com.zmt"/>
</beans>
@Component("userService")
@Lazy(true)//開啟懶加載
@Scope("singleton")//單例模式
public class UserServiceImpl implements UserService {public UserServiceImpl() {System.out.println("UserService被構造");}@PostConstructprivate void init(){System.out.println("執行init方法");}@PreDestroyprivate void destroy(){System.out.println("執行銷毀方法");}
}
測試代碼?
public class Test {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");Object bean = context.getBean("userService");System.out.println(bean);context.close();}
}
執行結果如下
UserService被構造
執行init方法
com.zmt.service.impl.UserServiceImpl@45afc369
執行銷毀方法
為了方便區分不同的業務層,@Component注解又衍生了三個注解
- @Service:Component的派生注解,多添加在Service的實現類上
- @Controller:Component的派生注解,多添加在Controller類上
- @Repository:Component的派生注解,多添加在Dao實現類上
依賴注解的使用
Bean的依賴注入的注解,主要是替代xml中的<property>標簽中的注入操作
<bean id="" class=""><property name="" ref=""/><property name="" value=""/>
</bean>
Spring提供的注解如下,用于Bean內部進行屬性注入的
屬性注入注解 | 描述 |
@Value | 使用在字段或方法上,用于注入普通數據 |
@Autowired | 使用在字段或方法上,用于根據類型(byType)注入引用數據 |
@Qualifier | 使用在字段或方法上,結合@Autowired使用,根據名稱注入 |
@Resource | 使用在字段或方法上,根據類型或名稱進行注入 |
這些注解的工作原理實際上是通過暴力反射然后賦值,因此不需要set方法,但是添加了set方法在set方法上添加注解也可以使用。
下面是簡單的使用案例
@Value該注解可以對普通數據類型進行賦值,一種是直接指定需要注入的值,一種是通過占位符讀取需要注入的值信息
<?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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!--開啟自動掃描--><context:component-scan base-package="com.zmt"/><!--將test.txt文件加載到Spring中,供賦值使用--><context:property-placeholder location="classpath:test.txt"/>
</beans>
@Component("userService")
public class UserServiceImpl implements UserService {
// @Value("zhangsan")@Value("${name}")private String name;@Overridepublic void show() {System.out.println(name);}
}
以上兩種都可以將值賦值給name變量,但前者使用的意義不大,因此通常我們使用后者去讀取配置文件中的值。
@Autowired的使用默認是根據類型注入,但如果相同類型存在多個,則根據名稱進行注入,但是如果不存在屬性變量名的Bean對象,那么注入失敗
@Component("userService")
public class UserServiceImpl implements UserService {@Autowiredprivate UserDao userDao;@Autowiredpublic void setUserDao(UserDao userDao) {this.userDao = userDao;}//以上兩種寫一個即可@Overridepublic void show() {}
}
@Qualifier需要搭配@Autowired注解使用,特定指定在多個相同類型的Bean對象時,注入哪個名稱的bean對象
@Component("userService")
public class UserServiceImpl implements UserService {@Autowired@Qualifier("userDao")private UserDao userDao2;@Overridepublic void show() {}
}
這里即使屬性變量名為userDao2但是實際上注入的還是名為userDao的bean對象。
@Resource既可以類型注入也可以名稱注入,如果指定名稱,那么只能根據名稱注入
@Component("userService")
public class UserServiceImpl implements UserService {@Resource(name = "userDao")private UserDao userDao2;//實際上注入的是名稱為userDao的bean對象@Overridepublic void show() {}
}
@Autowired的擴展使用,實際上該注解不僅僅可以添加在set方法上,可以添加在任何方法上,下面是一個測試案例
@Component("userService")
public class UserServiceImpl implements UserService {@Overridepublic void show() {}@Autowiredpublic void xxx(UserDao userDao){System.out.println("xxx:"+userDao);}@Autowiredpublic void yyy(List<UserDao> userDaoList){System.out.println("yyy:"+userDaoList);}
}
運行結果如下
非自定義Bean的注解開發
對于非自定義的Bean我們需要使用工廠來進行加載,需要使用@Bean注解,如果需要參數,對于基本類型需要@Value注解,對于引用類型,只要Spring中存在,可以直接根據類型注入。
@Component
public class OtherBean {@Beanpublic DataSource dataSource(@Value("${name}") String name){DataSource dataSource = new DataSource();System.out.println(name);return dataSource;}
}