Spring
1.1.1.1 创立一个 bean
package com.zt.spring;
public class MyBean {
private String userName;
private Integer userAge;
}
1.1.1.2 配置 Config 配置 bean
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();
}
}
1.1.1.3 配置 bean 单例还是多例的
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();
}
}
1.1.1.4 获取上文,获取 bean
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();}
}
1.1.2 通过 FactoryBean 实现 bena 的拆卸
1.1.2.1 创立一个 bean
package com.zt.spring;
public class Car {}
1.1.2.2 配置 Config 配置 bean
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();
}
}
1.1.2.3 配置 FactoryBean 生产 bean
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;}
}
1.1.2.4 获取上文,获取 bean
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();}
}
1.1.3 通过另外一种工工厂模式实现 bena 的拆卸
1.1.3.1 创立一个 bean
package com.zt.spring;
public class Jeep {}
1.1.3.2 配置 Config 配置 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) {return factory.creat();
}
}
1.1.3.3 创立 Factory 实现构建办法
package com.zt.spring;
public class JeepFactory {public Jeep creat() {return new Jeep();
}
}
1.1.3.4 获取上文,获取 bean
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();}
}
1.2 spring 中 bean 的销毁办法
1.2.1 调用 spring 本人的初始化和销毁的办法
1.2.1.1 创立一个 bean
并且继承 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========");
}
}
1.2.1.2 在 conffig 文件中拆卸这个 bean
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();
}
}
1.2.1.3 获取上下文
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();}
}
1.2.2 调用自定义初始化和销毁的办法
1.2.2.1 创立一个 bean
自定义 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========");
}
}
1.2.2.2 在 conffig 文件中拆卸这个 bean
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();
}
}
1.2.2.3 获取上下文
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();}
}
1.2.3 调用自定义并且注解初始化和销毁的办法
1.2.3.1 创立一个 bean
自定义 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========");
}
}
1.2.3.2 在 conffig 文件中拆卸这个 bean
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();
}
}
1.2.2.3 获取上下文
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();}
}
1.4 pom. 文件
<?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 相干技术文章或行业资讯,欢送大家关注和转发文章!