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相關技術文章或行業資訊,歡迎你們關注和轉發文章!java