bean的實例化
1.導入jar包(必不可少的)
2.實例化bean
- applicationContext.xml(xml的寫法)
<bean id="userDao" class="com.igeekhome.dao.impl.UserDao"></bean>
- 注解的寫法
第一種:在 applicationContext.xml中開啟注解掃描(同時引入context命名空間)
<?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 配置spring ioc容器開啟注解掃描,掃描指定包下的@Component修飾的類,將這些類的對象創建交給spring ioc容器完成
base-package: 需要掃描哪些包(及其子包)
-->
<context:component-scan base-package="com.igeekhome"></context:component-scan>
</beans>
第二種:@Component、@Service、@Controller、@Repository 用於實例化bean
Spring3.0為我們引入了組件自動掃描機制,它可以在類路徑底下尋找標注了@Component、@Service、@Controller、@Repository注解的類,並把這些類納入進spring容器中管理
@Service、@Controller、@Repository是@Component衍生的子注解
- @Repository用於標注數據訪問組件(如DAO層組件)
- @Service用於標注業務層組件(如Service層)
- @Controller用於標注控制層組件(如struts中的action層)
- @Component泛指組件,當組件不好歸類的時候,我們可以使用這個注解進行標注
/*
注解:添加在實現類上(思想同配置文件)
@Component 等價於 <bean id="userDaoImpl" class="com.igeekhome.dao.impl.UserDaoImpl"></bean>
id:默認是類名(首字母小寫) value:bean的id
@Component(value = "userDao")
@Component("userDao")
*/
@Repository("userDao")
public class UserDaoImpl implements IUserDao {
public void select() {
System.out.println("UserDao...select...");
}
}
bean屬性的依賴注入
簡單數據類型依賴注入
Spring3.0后,提供 @Value注解,可以完成簡單數據的注入
/*
@Component
@Component(value="customerService")
@Service(value="customerService")
四者都等價
*/
@Service("customerService")
public class CustomerService {
//簡單類型的成員變量
@Value("Rose")//參數的值簡單類型
private String name="Jack";
//保存業務方法
public void save(){
System.out.println("CustomerService業務層被調用了。。。");
System.out.println("name:"+name);
}
}
復雜類型數據依賴注入
1.使用@Value 結合SpEL -- spring3.0 后用
@Service("userService")
public class UserServiceImpl implements IUserService {
/*<bean id="userDao" class="com.igeekhome.dao.impl.UserDao"> </bean>
@Value("#{bean的id}")
*/
//寫法一 (在屬性聲明上面注入,底層自動還是生成setUserDao())
@Value("#{userDao}")
private UserDaoImpl userDao;
//寫法二
@Value("#{userDao}")
public void setUserDao(UserDaoImpl userDao) {
this.userDao = userDao;
}
public void list() {
System.out.println("UserServiceImpl...list...");
userDao.select();
}
}
2.使用@Autowired 結合 @Qualifier
@Autowired:可以單獨使用,如果單獨使用,按照類型進行注入(會在spring ioc容器中查找類型為com.igeekhome.dao.impl.UserDaoImpl的bean並進行注入,如果找不到,肯定注入失敗;如果找到匹配的單個bean,則注入成功; 如果找到多個相同類型的bean,則會選擇其中一個bean進行注入
@Qualifier:與@Autowired結合,按照名稱進行注入
@Autowired//默認按照類型注入的
@Qualifier("userDao")//必須配合@Autowired注解使用,根據名字注入
private UserDaoImpl userDao;
3.JSR-250標准(基於jdk) 提供注解@Resource
如果沒有指定name屬性,那么先按照名稱(注解修飾的屬性名)進行注入,在容器中查找bean的name/id為userDao的bean,如果找到,則注入成功。如果按照名稱進行注入沒有找到相對應的bean,那么就會使用按照類型進行裝配,如果沒有改類型,則注入失敗,拋出異常;如果容器中存在單個該類型的bean,則注入成功;如果容器中存在多個相同類型bean,則注入失敗(expected single matching bean but found 2: userDaoxxx,userDaox)
如果指定了name屬性,那么就只會按照name進行注入
@Resource//(name="userDao")
private UserDaoImpl userDao;
4.JSR-330標准(jdk) 提供 @Inject和@Named注解(不推薦)
需要先導入 javax.inject 的 jar
使用@Inject,默認按照類型注入,使用@inject和@Named注解,則按照名稱注入。用法與@Autowired 結合 @Qualifier 的用法一致
bean的初始化和銷毀
1.applicationContext.xml 中的寫法
<!--
init-method:指定初始化方法
destroy-method: 指定銷毀觸發方法
-->
<bean id="lifecycle" class="com.igeekhome.bean.LifeCycleBean" scope="singleton" init-method="initMethod" destroy-method="destroyMethod"></bean>
2.注解寫法
使用 @PostConstruct 注解, 標明初始化方法 ---相當於 init-method 指定初始化方法
使用 @PreDestroy 注解, 標明銷毀方法 ----相當於 destroy-method 指定對象銷毀方法
@Repository("userDao")
public class UserDaoImpl implements IUserDao {
public void select() {
System.out.println("UserDao...select...");
}
//init-method
@PostConstruct
public void init() {
System.out.println("init...");
}
//destory-method
@PreDestroy
public void destory() {
System.out.println("destory");
}
}
bean的作用域
1.applicationContext.xml 中的寫法
默認是 singleton 單例 ,prototype是多例
<bean id=”” class=”” scope=”prototype”>
2.注解寫法
通過@Scope注解,指定Bean的作用域
@Service("userService")
//@Scope("singleton")默認是單列
@Scope("prototype")
public class UserServiceImpl implements IUserService { }
備注
只有在Spring配置文件中開啟了注解掃描
才能使用 @Component @Autowired @Resource @PostConstruct @PreDestroy等注解