Spring uses annotations to implement IOC and DI (control inversion and dependency injection)

Source: Internet
Author: User

Spring uses annotations to implement IOC and Di


Annotations in a way that implements Ioc&di
1. IOC operation
1) @Component: Used to implement IOC control inversion, attribute value is used to implement object ID definition
2) @Scope: For the configuration of single case, multiple cases, request,session, etc.

3) @Controller: annotations specifically for controller layers for control inversion, property value
4) @Service: Annotations specifically for Service layer, control inversion, property value
5) @Repository: Annotations for persistence layer, control inversion, property value

2. Di operation
1) @Resource (name= "UserService")--Implement dependency injection
Retrieved first by name plus type, followed by name or True type only one item, followed by name and type omitted, then by name

2) @Autowired---Automatic assembly, the automatic assembly implemented by this annotation is injected according to the class implementation, if there is a class with the same name, then this annotation mode does not recognize which class implementation injection, at this time
An exception can occur and, in order to avoid an exception, it is possible to use @qualifier (value= "es") (familiarity) to implement the helper, that is, Qualifier can indicate the object ID


Here is the demo code:

The project structure is as follows:



Required JAR Packages:



1. Useraction.java Action Layer

Package cn.sz.action;

Import Javax.annotation.Resource;

Import Org.springframework.context.annotation.Scope;
Import Org.springframework.stereotype.Controller;

Import Cn.sz.service.IUserService;

@Controller
@Scope (value = "prototype") public
class Useraction {
	@Resource (name = "Userserviceimpl")
	private Iuserservice Userserviceimpl;

	public void Save () {

	}
}


2. Iuserdao.java Persistent Layer Interface

Package Cn.sz.dao;

Public interface Iuserdao {
	//Add method public
	void Save ();
}


3. Userdaoimpl.java Persistence Layer Interface implementation class

Package Cn.sz.dao.impl;

Import org.springframework.stereotype.Repository;

Import Cn.sz.dao.IUserDAO;

@Repository (value = "Userdaoimpl") Public
class Userdaoimpl implements Iuserdao {

	@Override public
	Void Save () {

	}

}


4, Iuserservice.java service layer interface

Package cn.sz.service;

Public interface Iuserservice {
	//Add method public
	void Save ();
}


5, Userserviceimpl.java Service Layer Interface implementation class

Package Cn.sz.service.impl;

Import Javax.annotation.Resource;

Import Org.springframework.stereotype.Service;

Import Cn.sz.dao.IUserDAO;
Import Cn.sz.dao.impl.UserDAOImpl;
Import Cn.sz.service.IUserService;

@Service (value = "Userserviceimpl") Public
class Userserviceimpl implements Iuserservice {
	@Resource (name = " Userdaoimpl ", type = userdaoimpl.class)
	private Iuserdao Userdaoimpl;

	@Override public
	Void Save () {

	}

}


6. Applicationcontext.xml Spring Master profile (for annotation support configuration and annotation scanning)

<?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/http Www.springframework.org/schema/context/spring-context.xsd >
	<!--annotations Support-
	<context: Annotation-config></context:annotation-config>
	<!--scanning: Classes in the current package and <context in the child package
	: Component-scan base-package= "Cn.sz" ></context:component-scan>
</beans>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.