Spring Annotations light into the light - do not brag force is not loaded to force
Recap
The instruments we say " Spring light into the light ," Spring's core idea read Part friends should already know, Cipian spend bedding articles, the introduction of notes, for further study.
The introduction of notes
In the Spring framework, despite the use of XML configuration files can be simply fitted Bean, but if there are a large number of applications need to assemble Bean, will lead to an XML configuration file is too large, the future is not convenient to upgrade and maintain, therefore, we recommend developers use Notes ways to assemble Bean.
Detailed notes
Of notes is not clear you can see my blog " Notes ", in fact, is a class annotated, use annotations replace the XML configuration file in development.
1. @Component substituted <bean class = ""> </ the bean>
@Component ( "ID value")
2. Web development, provides annotations derived annotation @Component 3 (function as) a substituted <bean class = "" > </ bean>
the @Repository: DAO layer
@Service: service layer
@Controller: web layer
Note: SpringMVC recommended using annotations Oh!
3. dependency injection, is provided to the private field, may be provided to setter methods
Normal values: @Value ( "")
reference value:
Embodiment 1: [type] according to the injection
@Autowired
Embodiment 2: [type + according to Title 1] implanted
@ Autowired is
@Qualifier ( "name")
mode 3: injection according to [name] 2
@Resource ( "name")
4. life cycle
@PostConstruct: initialization
@PreDestroy: destruction
5.
Configuration Notes
With the use of annotations Bean implementation class, but now does not know where to go Spring container scanning Bean object, you need to configure comment in the configuration file.
<?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:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <-! using the context namespace specified by Spring scanning and annotation sub-packet all packages Bean implementation class, annotated parse -> <context: Component Base-package-Scan = "com.my"> </ context: Scan-Component> </ Beans>
Practice Case
A, Pom.xml
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <!--spring核心依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.1.5.RELEASE</version> </dependency> </dependencies>
二、spring-config.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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc. XSD "> ! <- annotations arranged to scan packages -> <context: Component Base-package-scan =" com.my "> </ context: scan-Component> </ Beans>
三、UserDao
package com.my.dao; public interface UserDao { public void add(); public void delete(); public void update(); public void query(); }
Four, UserDaoImpl
com.my.dao.impl Package; Import com.my.dao.UserDao; Import org.springframework.stereotype.Repository; @Repository public class UserDaoImpl the implements UserDao { @Override public void the Add () { System.out.println ( " add functionality in UserDao achieved "); } @Override public void delete () { System.out.println (" deletion function is achieved UserDao "); } @Override public void Update () { System.out.println ( "the editing UserDao achieved"); } @Override public void query () { System.out.println ( "search function in UserDao achieved"); } }
Five, UserService
package com.my.service; public interface UserService { public void add(); public void delete(); public void update(); public void query(); }
Six, UserServiceImpl
package com.my.service.impl; import com.my.dao.UserDao; import com.my.dao.impl.UserDaoImpl; import com.my.service.UserService; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService { private UserDao userDao = new UserDaoImpl(); @Override public void add() { userDao.add(); } @Override public void delete() { userDao.delete(); } @Override public void update() { userDao.update(); } @Override public void query() { userDao.query(); } }
七、UserController
package com.my.controller; import com.my.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.stereotype.Service; @Controller public class UserController { @Autowired private UserService userService; public void add(){ userService.add(); } public void delete(){ userService.delete(); } public void update(){ userService.update(); } public void query(){ userService.query(); } }
Eight, test Test2
package com.my.test; import com.my.controller.UserController; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test2 { public static void main(String[] args) { ApplicationContext appCon = new ClassPathXmlApplicationContext("spring-config.xml"); UserController userController = (UserController) appCon.getBean("userController"); userController.add(); userController.delete(); userController.update(); userController.query(); } }
Nine, the test results
End summary
After reading this post and articles on Bean's friends should have two common assembly methods, which is based on assembly and annotation of assembly XML-based configuration, which Cipian annotation-based assembly method is particularly important, it is the current mainstream assembly method.
*****************************************************************************************************
My blog park Address: https://www.cnblogs.com/zyx110/
Reprint please indicate the source
I can not guarantee what I said is right, but I can guarantee that every one is hard to write, I always agree "to share more, the greater your value added" I welcome attention to technology-sharing "Java horse world" experience to share and learn "horse world", progress in the sharing, the harder the more fortunate in life to win the turning point, change from now on!
Support my friends who remember the point wave recommendation Oh, you sure it is my motivation.