java开发两年,连Spring中bean的装配都不知道?你怎么涨薪啊
2021-04-10 02:26
标签:new ica scope err 默认 实体 xmlns 模式 group 感谢你看到这里,说的都是自己的一些看法和见解,如有不对,请指正!觉得文章对你有帮助的话不妨给我点个赞,每天都会分享java相关技术文章或行业资讯,欢迎大家关注和转发文章! java开发两年,连Spring中bean的装配都不知道?你怎么涨薪啊 标签:new ica scope err 默认 实体 xmlns 模式 group 原文地址:https://blog.51cto.com/14801695/25129371.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
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.文件
最后
上一篇:C++语言关键字及注解
文章标题:java开发两年,连Spring中bean的装配都不知道?你怎么涨薪啊
文章链接:http://soscw.com/index.php/essay/73588.html