http://cache.baiducontent.com/c?m= 9f65cb4a8c8507ed4fece7631046893b4c4380143fd3d1027fa3c215cc79031c1061e5bc23251100ce95223a54b2081ab9b66d232a0927b69ece8d4fd cbb902b2f8927347716804214d212b2df037881769f4d99aa0e97bce74398b9a5d5c85523dd22766df0f79c2b0203be19e71541f4d6e95f662b07bb9d 2715f84e062f882230a136fbf7456c108086ca2a48d45cd27610e7b843b52961b504d4690c5344b74dc11f272327934f308e4e2a73e2fc5c973d09303 4c14ba4b8b1a19a439ba89926eef8dcdc2a&p=cb6adc0c8c934eae5bf5c771075e&newp= 882a9446809b17fe13be9b744f5292695803ed6338d5864d2985d8&user=baidu&fm=sc&query=%b7%b4%c9%e4%ca%b5% C0%fd%bb%af%b5%c4%c0%e0%d6%d0%d7%a2%bd%e2%d7%a2%c8%eb%bb%e1%c9%fa%d0%a7&qid=&p1=1
The Spring IOC container will first instantiate all of the beans, whether it is used by the rat or not, if you want to temporarily not instantiate The bean, use the attribute
Lazy-init= "true".
Three ways to inject spring:
① Construction Injection: Injected through the constructor (constructor)
② setpoint injection: Injection by setter method
③ Reflection Injection: Injected by annotation (annotation)
Spring four automatic assembly (injection) method for beans
Autowire= "ByName": Value injection of attributes through the bean's name
Autowire= "Bytype": Value injection of the property through the class type of the property. < caution >
Autowire= "constructor": Value injection of the property through the constructor. < caution >
Autowire= "AutoDetect": the container automatically injects values into the property, using the constructor method first, if there is no constructor, then Bytype. < try not to use >
Automatic injection of beans by annotations:
@Autowired : The bean attribute is automatically injected by "Bytype", if there are multiple classes of bean properties, add @Qualifier("Beanname") Be differentiated.
@Resource: The bean attribute is automatically injected through "Bytype", if there are multiple classes of bean properties, then @resource ("Beanname") is used.
The @Resource ("Beanname") is an automatic injection of bean properties by means of "byname".
Application scope of Spring bean
Scope= "Singleton": Singleton (default), only one instance is generated for all apps
Scope= "prototype": generates an instance for each app
Scope= "Request": valid in a web app, generating an instance for each request
Scope= "Session": Valid in Web applications, generating an instance for each session
Scope= "Global-session": Valid in Web app, global http session
Spring's IOC components :
@Repository: A durable layer component that is used to label data access layer components, such as DAO layer components.
@Service: Serves as a component for labeling business layer components, such as service layer components ; an instance of a lowercase bean is instantiated based on The Bean's class type, if you want to modify the bean Name can be in @service ("Custome beanname").
@Controller: The action layer component that is used to label the control-layer primary key, such as Strust.
@Component: A generic component, which can be used when the component is poorly categorized.
When you use the annotation above, you do not need to define the bean in Applicationcontext.xml.
examples :
<?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 ">
<!--fully Annotated--
<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>
<!--define the transaction manager (declarative transaction)-->
<bean id= " transactionmanager " class= " Org.springframework.orm.hibernate3.HibernateTransactionManager,
<property name= "SessionFactory "Ref=" Sessionfactory "/>
</bean>
&NBSP
<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;
//Omit getter and setter
}
@Repository ("Customerdao")
Public class Customerdao {
@Resource
PrivateHibernatetemplatehibernatetemplate;
PublicCustomer Findbyprimarykey (LongCUSTOMERID) {
return(Customer)hibernatetemplate. Get (Customer.class, customerId);
}
Public voidSave (Customer customer) {
hibernatetemplate. Save (customer);
}
Public voidUpdate (Customer customer) {
hibernatetemplate. Update (customer);
}
}
@Service
@Transactional (readonly=true)
Public class CustomerService {
@Autowired
@Qualifier ("Customerdao")
PrivateCustomerdaoCustomerdao;
PublicCustomer Findbyprimarykey (LongCUSTOMERID) {
returnCustomerdao. 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);
}
}
Spring Reflection injection + full annotation injection