Spring IoC容器會先把全部的Bean都進行實例化,無論是要用到的火鼠用不到的,若是你想暫時不進行Bean的實例化,要用到屬性
lazy-init="true".web
Spring的三種注入方式:spring
① 構造注入:經過構造器(constructor)注入session
② 設值注入:經過Setter方法注入app
③ 反射注入:經過註解(annotation)的方式注入spa
Spring 對Bean的四種自動裝配(注入)方式hibernate
autowire= "byName" :經過Bean的名字對屬性進行值注入prototype
autowire="byType":經過屬性的類型來對屬性進行值注入。<慎用>component
autowire="constructor":經過構造器來對屬性進行值注入。<慎用>orm
autowire="autodetect":容器自動對屬性進行值注入,先用constructor的方式,若是沒有構造器,再用byType的方式。<儘可能不用>xml
經過註解的方式對Bean的自動注入:
@Autowired :是經過"byType"的方式對Bean屬性進行自動注入的,若是Bean屬性的類型有多個,那麼就添加@Qualifier("beanName") 加以區分。
@Resource:是經過"byType"的方式對Bean屬性進行自動注入的,若是Bean屬性的類型有多個,那麼就用@Resource("beanName") ,
@Resource("beanName") 是經過"byName"的方式對Bean屬性進行自動注入的。
Spring Bean的應用範圍
scope="singleton":單例(默認),對全部應用都只生成一個實例
scope="prototype":對每一個應用都生成一個實例
scope="request":在web應用中有效,對每一個請求都生成一個實例
scope="session":在web應用中有效,對每一個會話都生成一個實例
scope="global-session":在web應用中有效,全局Http會話
Spring的IoC組件:
@Repository:持久層組件,用於標註數據訪問層組件,如DAO層組件。
@Service:服務成組件,用於標註業務層組件,如Service層組件;會根據Bean的類型實例化一個首字母爲小寫的bean的實例,若是要修改bean name能夠在@Service("custome beanName")。
@Controller:用於標註控制層主鍵,如Strust的Action層組件。
@Component:泛指組件,當組件很差歸類的時候能夠用這個標註。
當用了上面的annotation的時候就不須要再在applicationContext.xml定義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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--全註解-->
<context:annotation-config />
<context:component-scan base-package="com.demo.service" />
<context:component-scan base-package="com.demo.dao" />
<context:component-scan base-package="com.demo.controller" />
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
</bean>
<!-- 定義事務管理器(聲明式的事務) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="hibernateTempate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
public class Customer {
private Long customerId;
private String name;
//省略getter 和 setter
}
@Repository("customerDAO")
public class CustomerDAO {
@Resource
private HibernateTemplate hibernateTemplate;
public Customer findByPrimaryKey(long customerId) {
return (Customer) hibernateTemplate.get(Customer.class, customerId);
}
public void save(Customer customer) {
hibernateTemplate.save(customer);
}
public void update(Customer customer) {
hibernateTemplate.update(customer);
}
}
@Service
@Transactional(readOnly=true)
public class CustomerService {
@Autowired
@Qualifier("customerDAO")
private CustomerDAO customerDAO;
public Customer findByPrimaryKey(long customerId) {
return customerDAO.findByPrimaryKey(customerId);
}
@Transactional(propagation=Propagation.REQUIRED)
public void save(Customer customer) {
customerDAO.save(customer);
}
@Transactional(propagation=Propagation.REQUIRED)
public void update(Customer customer) {
customerDAO.update(customer);
}
}
@Controller
public class CustomerController {
@Resource
CustomerService customerService;
public void modifyCustomerAndProduct() {
Customer customer = customerService.findByPrimaryKey(1);
customer.setName("joe");
customerService.update(customer);
}
}