package com.zt.spring; public class MyBean { private String userName; private Integer userAge; }
package com.zt.spring; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MyBeanConfig { // 和xml中配置文件的bean的标签是一样的 @Bean(name = "beanName") public MyBean createBean(){ return new MyBean(); } }
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") // 默认值是多例的 xml的方式可以在bean的标签上面 设置这个参数 @Scope("prototype") public MyBean createBean(){ return new MyBean(); } }
package com.zt.spring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class App { public static void main(String[] args) { // 获取上下文 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class); // 通过bean的那么获取bean System.out.println(context.getBean("beanName")); // 获取bean的class文件获取 System.out.println(context.getBean(MyBean.class)); context.close(); } }
package com.zt.spring; public class Car { }
package com.zt.spring; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MyBeanConfig { @Bean public CarFactoryBean createRunnableFactoryBean() { return new CarFactoryBean(); } }
package com.zt.spring; import org.springframework.beans.factory.FactoryBean; public class CarFactoryBean implements FactoryBean<Car> { //获取到对应的实体 @Override public Car getObject() throws Exception { return new Car(); } // 返回的额对应的是class文件 @Override public Class<?> getObjectType() { return Car.class; } //配置是不是单例 @Override public boolean isSingleton() { return true; } }
package com.zt.spring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class App { public static void main(String[] args) { // 获取上下文 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class); // 通过FactoryBean 创建的bean System.out.println(context.getBean(Car.class)); // 通过name获取car的实体 System.out.println(context.getBean("createRunnableFactoryBean")); context.close(); } }
package com.zt.spring; public class Jeep { }
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) { return factory.creat(); } }
package com.zt.spring; public class JeepFactory { public Jeep creat() { return new Jeep(); } }
package com.zt.spring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class App { public static void main(String[] args) { // 获取上下文 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class); System.out.println(context.getBean("createJeep")); System.out.println(context.getBean(Jeep.class)); context.close(); } }
并且继承InitializingBean, DisposableBean ,实现afterPropertiesSet,destroy方法
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========"); } }
package com.zt.spring; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MyBeanConfig { @Bean public User createUser() { return new User(); } }
package com.zt.spring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class App { public static void main(String[] args) { // 获取上下文 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class); System.out.println(context.getBean(User.class)); context.close(); } }
自定义init,destroy方法
package com.zt.spring; public class Dog { // 初始化方法 public void init() throws Exception { System.out.println("=======init========"); } // 销毁的方法 public void destroy() throws Exception { System.out.println("=======destroy========"); } }
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() { return new Dog(); } }
package com.zt.spring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class App { public static void main(String[] args) { // 获取上下文 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class); System.out.println(context.getBean(Dog.class)); context.close(); } }
自定义init,destroy方法 在加上注解放 @PostConstruct, @PreDestroy的方式实现
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========"); } }
package com.zt.spring; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MyBeanConfig { @Bean public UserInfo createUserInfo() { return new UserInfo(); } }
package com.zt.spring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class App { public static void main(String[] args) { // 获取上下文 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyBeanConfig.class); System.out.println(context.getBean(UserInfo.class)); context.close(); } }
<?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-SNAPSHOT</version> <properties> <project.build.sourceEncodding>UTF-8</project.build.sourceEncodding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.20.RELEASE</version> </dependency> </dependencies> </project>
感谢你看到这里,说的都是自己的一些看法和见解,如有不对,请指正!觉得文章对你有帮助的话不妨给我点个赞,每天都会分享java相关技术文章或行业资讯,欢迎大家关注和转发文章!
很遗憾的说,推酷将在这个月底关闭。人生海海,几度秋凉,感谢那些有你的时光。
原文 https://segmentfault.com/a/1190000023355464