1 component registration
1.1 @ configuration & @ Bean register Bean
1.1.1 traditional xml
- Person.java
package com.xuweiwei.spring.model; /** * @describe: * @author: Don't worry about the past, only laugh for the rest of my life. * @version: 1.0 */ public class Person { private String name; private Integer age; public Person() { } public Person(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
- 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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <bean id="person" class="com.xuweiwei.spring.model.Person"> <property name="name" value="Wei Wei Xu"/> <property name="age" value="27"/> </bean> </beans>
- test
package com.xuweiwei.sping; import com.xuweiwei.spring.model.Person; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath*:applicationContext.xml") public class SpringTest { @Autowired private Person person; @Test public void test(){ System.out.println(person); } }
1.1.2 annotation method
- Person.java
package com.xuweiwei.spring.model; /** * @describe: * @author: Don't worry about the past, only laugh for the rest of my life. * @version: 1.0 */ public class Person { private String name; private Integer age; public Person() { } public Person(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
- MainConfig.java
package com.xuweiwei.spring.config; import com.xuweiwei.spring.model.Person; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @describe: * @author: Don't worry about the past, only laugh for the rest of my life. * @version: 1.0 */ //Configuration class==configuration file //tell Spring This is a configuration class @Configuration //Amount to applicationContext.xml public class MainConfig { /** * @Bean Annotations are equivalent to bean Tags * * @return */ @Bean public Person person() { return new Person("Wei Wei Xu", 27); } }
- test
package com.xuweiwei.sping; import com.xuweiwei.spring.config.MainConfig; import com.xuweiwei.spring.model.Person; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class ConfigurationSpringTest { @Test public void test() { ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class); Person person = context.getBean(Person.class); System.out.println(person); String[] names = context.getBeanDefinitionNames(); for (String name : names) { System.out.println(name); } } }
1.2 @ComponentScan auto scan
1.2.1 traditional xml
- PersonController.java
package com.xuweiwei.spring.controller; import org.springframework.stereotype.Controller; /** * @describe: * @author: Don't worry about the past, only laugh for the rest of my life. * @version: 1.0 */ @Controller public class PersonController { }
- PersonService.java
package com.xuweiwei.spring.service; import org.springframework.stereotype.Service; /** * @describe: * @author: Don't worry about the past, only laugh for the rest of my life. * @version: 1.0 */ @Service public class PersonService { }
- PersonDao.java
package com.xuweiwei.spring.dao; import org.springframework.stereotype.Repository; /** * @describe: * @author: Don't worry about the past, only laugh for the rest of my life. * @version: 1.0 */ @Repository public class PersonDao { }
- 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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!--Just label it@Controller,@Service,@Component,@Repository Annotations are automatically injected into the IOC Container--> <context:component-scan base-package="com.xuweiwei.spring"></context:component-scan> </beans>
- test
package com.xuweiwei.sping; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath*:applicationContext.xml") public class SpringTest { @Autowired private ApplicationContext context; @Test public void test(){ String[] names = context.getBeanDefinitionNames(); for (String name : names) { System.out.println(name); } } }
1.2.2 annotation method
- Person.java
package com.xuweiwei.spring.model; /** * @describe: * @author: Don't worry about the past, only laugh for the rest of my life. * @version: 1.0 */ public class Person { private String name; private Integer age; public Person() { } public Person(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
- PersonDao.java
package com.xuweiwei.spring.dao; import org.springframework.stereotype.Repository; /** * @describe: * @author: Don't worry about the past, only laugh for the rest of my life. * @version: 1.0 */ @Repository public class PersonDao { }
- PersonService.java
package com.xuweiwei.spring.service; import org.springframework.stereotype.Service; /** * @describe: * @author: Don't worry about the past, only laugh for the rest of my life. * @version: 1.0 */ @Service public class PersonService { }
- PersonController.java
package com.xuweiwei.spring.controller; import org.springframework.stereotype.Controller; /** * @describe: * @author: Don't worry about the past, only laugh for the rest of my life. * @version: 1.0 */ @Controller public class PersonController { }
- MainConfig.java
package com.xuweiwei.spring.config; import com.xuweiwei.spring.model.Person; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; /** * @describe: * @author: Don't worry about the past, only laugh for the rest of my life. * @version: 1.0 */ //Configuration class==configuration file //tell Spring This is a configuration class @Configuration //Amount to applicationContext.xml @ComponentScan(value = "com.xuweiwei") public class MainConfig { /** * @Bean Annotations are equivalent to bean Tags * * @return */ @Bean public Person person() { return new Person("Wei Wei Xu", 27); } }
- test
package com.xuweiwei.sping; import com.xuweiwei.spring.config.MainConfig; import com.xuweiwei.spring.model.Person; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class ConfigurationSpringTest { @Test public void test() { ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class); Person person = context.getBean(Person.class); System.out.println(person); String[] names = context.getBeanDefinitionNames(); for (String name : names) { System.out.println(name); } } }
- If it is an xml method that only filters the classes annotated by the Controller, the code is as follows
<?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" 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-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!--Just label it@Controller,@Service,@Component,@Repository Annotations are automatically injected into the IOC Container, scan all by default--> <context:component-scan base-package="com.xuweiwei.spring" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>
- If the annotation method only filters the classes annotated by the Controller, the code is as follows
package com.xuweiwei.spring.config; import com.xuweiwei.spring.model.Person; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.stereotype.Controller; /** * @describe: * @author: Don't worry about the past, only laugh for the rest of my life. * @version: 1.0 */ //Configuration class==configuration file //tell Spring This is a configuration class @Configuration //Amount to applicationContext.xml @ComponentScan( value = "com.xuweiwei", useDefaultFilters = false, includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Controller.class)} ) public class MainConfig { /** * @return * @Bean Annotations are equivalent to bean Tags */ @Bean public Person person() { return new Person("Wei Wei Xu", 27); } }
1.3 @Scope annotation
- Set the scope of the component.
- Example:
- MainConfig.java
package com.xuweiwei.spring.config; import com.xuweiwei.spring.model.Person; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; /** * @describe: * @author: Don't worry about the past, only laugh for the rest of my life. * @version: 1.0 */ //Configuration class==configuration file //tell Spring This is a configuration class @Configuration //Amount to applicationContext.xml public class MainConfig { /** * * @see ConfigurableBeanFactory#SCOPE_PROTOTYPE * * @see ConfigurableBeanFactory#SCOPE_SINGLETON * * @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST * * @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION * By default, it's singleton * * singleton It's a single case. * prototype There are many cases. * request Create an instance at the same request * session Create an instance of the same Session * @return */ @Scope(value = "singleton") @Bean public Person person() { return new Person("Wei Wei Xu", 27); } }
-
- Testing
package com.xuweiwei.sping; import com.xuweiwei.spring.config.MainConfig; import com.xuweiwei.spring.model.Person; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class ConfigurationSpringTest { @Test public void test() { ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class); Person person1 = context.getBean(Person.class); Person person2 = context.getBean(Person.class); System.out.println(person1 == person2); } }
- By default, IOC container startup will call methods and create objects to put into the container, and each subsequent acquisition will be directly obtained from the container.
- If the scope value is prototype, the IOC container does not call the method when it starts, but creates and puts the object into the container every time it gets the object.
1.4 @Lazy annotation
- @Lazy is for a single instance Bean.
- Example:
package com.xuweiwei.spring.config; import com.xuweiwei.spring.model.Person; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Lazy; /** * @describe: * @author: Don't worry about the past, only laugh for the rest of my life. * @version: 1.0 */ //Configuration class==configuration file //tell Spring This is a configuration class @Configuration //Amount to applicationContext.xml public class MainConfig { /** * Lazy loading: objects are not created when the container is started, but only when it is first used * @return */ @Lazy @Bean public Person person() { System.out.println("Fill the container Bean^_^ ^_^ ^_^ ^_^"); return new Person("Wei Wei Xu", 27); } }
1.5 @Conditional annotation
- Inject beans into the container according to the conditions. [this annotation is widely used at the bottom of spring boot]
- Example:
- LinuxCondition.java
package com.xuweiwei.spring.condition; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.env.Environment; import org.springframework.core.type.AnnotatedTypeMetadata; /** * @describe: * @author: Don't worry about the past, only laugh for the rest of my life. * @version: 1.0 */ public class LinuxCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { Environment environment = context.getEnvironment(); String property = environment.getProperty("os.name"); if(property.contains("linux")){ return true; } return false; } }
-
- WindowsCondition.java
package com.xuweiwei.spring.condition; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.env.Environment; import org.springframework.core.type.AnnotatedTypeMetadata; /** * @describe: * @author: Don't worry about the past, only laugh for the rest of my life. * @version: 1.0 */ public class WindowsCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { Environment environment = context.getEnvironment(); String property = environment.getProperty("os.name"); if(property.contains("Windows")){ return true; } return false; } }
-
- MainConfig2.java
package com.xuweiwei.spring.config; import com.xuweiwei.spring.condition.LinuxCondition; import com.xuweiwei.spring.condition.WindowsCondition; import com.xuweiwei.spring.model.Person; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; /** * @describe: * @author: Don't worry about the past, only laugh for the rest of my life. * @version: 1.0 */ public class MainConfig2 { @Conditional(value = WindowsCondition.class) @Bean("bill") public Person person1(){ return new Person("Bill Gates",60); } @Conditional(value = LinuxCondition.class) @Bean("linus") public Person person2(){ return new Person("Linus",50); } }
-
- Testing
package com.xuweiwei.sping; import com.xuweiwei.spring.config.MainConfig2; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class ConfigurationSpringTest { @Test public void test() { ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig2.class); String[] names = context.getBeanDefinitionNames(); for (String name : names) { System.out.println(name); } } }