(In this case using maven project)
1. Entity three categories:
@Component public class Cat { public void voice(){ System.out.println("miaomiao~~~"); } }
@Component public class Dog { public void voice(){ System.out.println("wangwang~~~~"); } }
@Component public class User { //@Autowired //@Qualifier(value = "cat") @Resource private Cat cat; //@Resource @Resource private Dog dog; public User() { } public User(Cat cat, Dog dog) { this.cat = cat; this.dog = dog; } public Cat getCat() { return cat; } public Dog getDog() { return dog; } // public void setDog(Dog dog) { // this.dog = dog; // } // // public void setCat(Cat cat) { // this.cat = cat; // } }
2.spring configuration file: (applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--cat bean--> <!--<bean id="cat" class="com.xbf.pojo.Cat"/>--> <!--dog bean--> <!--<bean id="dog" class="com.xbf.pojo.Dog"/>--> <!-- 1.传统的方式: User bean --> <!--<bean id="user" class="com.xbf.pojo.User">--> <!--<property name="cat" ref="cat"/>--> <!--<property name="dog" ref="dog"/>--> Objective: To complete implementation bean container spring, and all the attributes assigned to 2. Automatic assembling by name:<-!-></ Bean><-! <-!-> the User bean completed injection Process: extracting from this class name attribute set method; then some spring Ruoyi container id of the bean and attribute names consistent<the bean ID = "User" class = "com.xbf.pojo.User" the autowire = "byName" /> -> <-! 3. Automatic assembly according to the type: The type of the attribute of the existing spring bean container in implanting find the corresponding bean, bean occur if the same type of error is reported -> <-! <bean ID = "User" class = "com.xbf.pojo.User" the autowire = "the byType" /> -> <! - =================== embodiment annotated using injection properties ================== ========================= -> <! - use as annotations injection properties: <context: annotation-config / > context file header <-! . 4 <-.! class attribute Autowired is written above @Autowired be assembled by property type, irrespective of the bean ID -> <-! can not write set method. -> <-! <Context: Annotation-config /> & <the bean ID = "User" class = "com.xbf.pojo. the User "/> -> ! <- 5.Autowired: automatically adapt according to the type of plus @Qualifier (not used alone) combined with the use of equivalent = the autowire" byName " -> <-! 6. The @Resource : according to the name attribute provides direct injection property; attribute injecting = "byname" according to the default Autowired; Autowired = "byType" the implanting attribute; -> <! - ====== ======== type automatic assembling ======================================= ==================== -> <! - scan package: replacement using the bean ID annotation -> <!- written in the name of the class top notes: @Controller: written on the control layer class @Service: written in the business layer class @Repository: written in the Dao layer class @Component: not according to three categories, use this comment -> <-! 6.-> < context: Scan-Component Base-Package = "com.xbf.pojo" /> <-! Profile just written sentence: <context: Component Base-Package-Scan = "package path"> context: component assembly scan base-package basic package, can specify multiple packages, separated by commas function: annotated scanning in the packet, and generates bean injection properties -> </ Beans > ! <- (class names and class id the correspondence relationship: ~~~ more interesting.) category, and ID correspondence table (default): class name: CAT CAT cAT CaT ID: CAT cAT caT caT analysis: key class name is the second letter, the second letter uppercase, lowercase initial capital letters does not matter. If the second lowercase letters, it first must be lowercase. After the second letter case does not matter. If you do not specify a value, spring container id rule, it is the class name first letter lowercase. <-! =============================================== ================================== class name and the id of the bean custom: @Component (value = " CAT ") ->
3. Test categories:
public class Test { @org.junit.Test public void test(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); User user = (User) context.getBean("user"); user.getCat().voice(); user.getDog().voice(); } }
4.pop.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xbf</groupId> <artifactId>test1</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- spring-webmvc spring包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.9.RELEASE</version> </dependency> <!--单元测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- spring-tx --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.14.RELEASE</ Version > </ dependency > </ the Dependencies > < Build > < Resources > <-! Hope that when maven (target) export project, we will be able to export the configuration and resources -> < Resource > < Directory > the src / main / Java </ Directory > < Includes > < the include > ** / *. Properties </ the include > < the include > ** / *. XML </ the include > </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
Reference blog: https: //www.cnblogs.com/kuotian/p/8795812.html