Spring
1.1.1.1 Creating a Bean
package com.zt.spring;
public class MyBean {
private String userName;
private Integer userAge;
}
Copy the code
1.1.1.2 Config Configures the bean
package com.zt.spring; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @configuration public class MyBeanConfig {@bean (name =) is the same as the bean in XML Configuration file"beanName")
public MyBean createBean() {returnnew MyBean(); }}Copy the code
1.1.1.3 Is the Configuration bean Singleton Multi-instance
package com.zt.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
public class MyBeanConfig {
@Bean(name = "beanName"// The default value is multicase XML. You can set this parameter @scope () on the bean label."prototype")
public MyBean createBean() {returnnew MyBean(); }}Copy the code
1.1.1.4 Obtain above, obtain bean
package com.zt.spring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; Public class App {public static void main (String [] args) {/ / get the context AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class); // Get the bean system.out.println (context.getbean ("beanName")); System.out.println(context.getBean(mybean.class)); context.close(); }}Copy the code
1.1.2 Achieve bena assembly through FactoryBean
1.1.2.1 Creating a Bean
package com.zt.spring;
public class Car {
}
Copy the code
1.1.2.2 Configuring Config Configures beans
package com.zt.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBeanConfig {
@Bean
public CarFactoryBean createRunnableFactoryBean() {
returnnew CarFactoryBean(); }}Copy the code
1.1.2.3 Configuring FactoryBean Produces beans
package com.zt.spring; import org.springframework.beans.factory.FactoryBean; Public class CarFactoryBean implements FactoryBean<Car> {@override public Car getObject() throws Exception {returnnew Car(); } // Override public class <? >getObjectType() {
returnCar.class; } // The configuration is not singleton @override public BooleanisSingleton() {
return true; }}Copy the code
1.1.2.4 Obtain above, obtain bean
package com.zt.spring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; Public class App {public static void main (String [] args) {/ / get the context AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class); System.out.println(context.getBean(car.class)); System.out.println(context.getbean (context.getbean))"createRunnableFactoryBean")); context.close(); }}Copy the code
1.1.3 Assembly of BENA is realized through another factory mode
1.1.3.1 Creating a bean
package com.zt.spring;
public class Jeep {
}
Copy the code
1.1.3.2 Configuring Config Configures the bean
package com.zt.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBeanConfig {
@Bean
public JeepFactory createJeepFactory() {
return new JeepFactory();
}
@Bean
public Jeep createJeep(JeepFactory factory) {
returnfactory.creat(); }}Copy the code
1.1.3.3 Creating Factory Implements the construction method
package com.zt.spring;
public class JeepFactory {
public Jeep creat() {
returnnew Jeep(); }}Copy the code
1.1.3.4 Obtaining the above, obtain the bean
package com.zt.spring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; Public class App {public static void main (String [] args) {/ / get the context AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class); System.out.println(context.getBean("createJeep")); System.out.println(context.getBean(Jeep.class)); context.close(); }}Copy the code
1.2 Spring bean destruction methods
1.2.1 Calls Spring’s own initialization and destruction methods
1.2.1.1 Creating a Bean
And inherit InitializingBean, DisposableBean, implement afterPropertiesSet, destroy methodsCopy the code
package com.zt.spring;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class User implements InitializingBean, DisposableBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("=======afterPropertiesSet========");
}
@Override
public void destroy() throws Exception {
System.out.println("=======destroy========"); }}Copy the code
1.2.1.2 Assemble the bean in a Conffig file
package com.zt.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBeanConfig {
@Bean
public User createUser() {
returnnew User(); }}Copy the code
1.2.1.3 Obtaining context
package com.zt.spring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; Public class App {public static void main (String [] args) {/ / get the context AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class); System.out.println(context.getBean(User.class)); context.close(); }}Copy the code
1.2.2 Calling custom initialization and destruction methods
1.2.2.1 Creating a Bean
Custom init, destroy methodsCopy the code
package com.zt.spring; Public class Dog {// Initialization method public void init() throws Exception {system.out.println ("=======init========"); Public void destroy() throws Exception {system.out.println () {system.out.println ("=======destroy========"); }}Copy the code
1.2.2.2 Assemble the bean in a Conffig file
package com.zt.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBeanConfig {
@Bean(initMethod = "init",destroyMethod = "destroy")
public Dog createDog() {
returnnew Dog(); }}Copy the code
1.2.2.3 Obtaining context
package com.zt.spring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; Public class App {public static void main (String [] args) {/ / get the context AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class); System.out.println(context.getBean(Dog.class)); context.close(); }}Copy the code
1.2.3 Call custom and annotation initialization and destruction methods
1.2.3.1 Creating a Bean
Custom init/destroy methods are added to the @postConstruct / @predestroy implementationCopy the code
package com.zt.spring;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class UserInfo {
@PostConstruct
public void afterPropertiesSet() throws Exception {
System.out.println("=======afterPropertiesSet========");
}
@PreDestroy
public void destroy() throws Exception {
System.out.println("=======destroy========"); }}Copy the code
1.2.3.2 Assemble the bean in a Conffig file
package com.zt.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBeanConfig {
@Bean
public UserInfo createUserInfo() {
returnnew UserInfo(); }}Copy the code
1.2.2.3 Obtaining context
package com.zt.spring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; Public class App {public static void main (String [] args) {/ / get the context AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class); System.out.println(context.getBean(UserInfo.class)); context.close(); }}Copy the code
1.4 pom file
<? 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. Zt < / groupId > < artifactId > spring < / artifactId > < version > 1.0 - the SNAPSHOT < / version > < properties > < project. Build. SourceEncodding > utf-8 < / project. Build. SourceEncodding > < maven.com piler. Source > 1.8 < / maven.com piler source > < maven.com piler target > 1.8 < / maven.com piler. Target > < / properties > < dependencies > < the dependency > < the groupId > org. Springframework < / groupId > < artifactId > spring - the context < / artifactId > < version > 4.3.20. RELEASE < / version > </dependency> </dependencies> </project>Copy the code
The last
Thank you to see here, said are some of their own views and insights, if there is wrong, please correct! If you think the article is helpful to you, you might as well give me a thumbs up. Every day, we will share Java related technical articles or industry information. Welcome to pay attention to and forward the article!