前端时间找到一个spring的学习视频,觉得不错,现在写个博客作为笔记
准备工作:
工具:idea
jdk1.8
maven
pom文件
<properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.2.RELEASE</version> </dependency> </dependencies>
bean的装配有3种方式
第一种
/** *这是一个bean */ public class MyBean { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
//配置类 @Configuration public class MyConfig { // bean的装配方式一 @Bean(name = "MyBean") //默认单例 //@Scope("prototype") public MyBean getMyBean() { MyBean myBean = new MyBean(); myBean.setName("SFZ"); return myBean; } }
public class App { public static void main(String[] args) { //注解,这里使用配置类作为参数 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class); //根据类型获取 MyBean bean = context.getBean(MyBean.class); System.out.println("bean = " + bean); //根据方法名字获取 //Object getMyBean = context.getBean("getMyBean"); //System.out.println("getMyBean = " + getMyBean); //自定义名字 Object myBean = context.getBean("MyBean"); System.out.println("myBean = " + myBean); context.close(); } }
这是bean的第一种装配方式,从这可以看到,可以根据类型和名字获取bean,@Bean注解标注的方法是默认的名字,可以通过@Bean(name="beanName")指定,
可以使用@Scope 指定是否单例.默认单例
第二种
public class JeepFactoryBean implements FactoryBean<Jeep> { //获取实例对象 @Override public Jeep getObject() throws Exception { return new Jeep(); } //返回对象类型 @Override public Class<?> getObjectType() { return Jeep.class; } //是否单例 @Override public boolean isSingleton() { return false; } }
public class Jeep { }
//配置类 @Configuration public class MyConfig2 { // bean的装配方式二 @Bean public JeepFactoryBean createJeepFactoryBean() { return new JeepFactoryBean(); }
}
public class App2 { public static void main(String[] args) { //注解 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig2.class); //根据类型获取工厂bean JeepFactoryBean jeepFactoryBean = context.getBean(JeepFactoryBean.class); System.out.println("jeepFactoryBean = " + jeepFactoryBean); //根据名字获取要加一个"&"符号,详细见源码:BeanFactory.class 常量FACTORY_BEAN_PREFIX Object bean = context.getBean("&createJeepFactoryBean"); System.out.println("bean = " + bean); //根据类型获取bean Jeep jeep = context.getBean(Jeep.class); //根据名字获取,默认方法名字 Object createRunnableFactoryBean = context.getBean("createJeepFactoryBean"); System.out.println("jeep = " + jeep); System.out.println("createRunnableFactoryBean = " + createRunnableFactoryBean); } }
第三种
public class CatFactory { public Cat create() { Cat jeep = new Cat(); return jeep; } }
public class Cat { }
//配置类 @Configuration public class MyConfig3 { //bean的装配方式三 @Bean public CatFactory createJeepFactory() { return new CatFactory(); } @Bean public Cat createCat(CatFactory catFactory) { Cat cat = catFactory.create(); return cat; } }
public class App3 {
public static void main(String[] args) {
//注解
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig3.class);
Cat cat = context.getBean(Cat.class);
System.out.println("cat = " + cat);
Object createCat = context.getBean("createCat");
System.out.println("createCat = " + createCat);
CatFactory bean = context.getBean(CatFactory.class);
System.out.println("bean = " + bean);
context.close();
}
}
这就是bean的三种装配方式,可以不再使用xml来配置.
bean的初始化的和销毁的方法
有三种方式,
第一:
/** * InitializingBean:容器初始化执行 * DisposableBean:容器结束执行 */ public class Cat implements InitializingBean, DisposableBean { @Override public void afterPropertiesSet() throws Exception { System.out.println("实现InitializingBean接口,实现bean初始化之后执行的操作"); } @Override public void destroy() throws Exception { System.out.println("实现DisposableBean接口,实现bean销毁之前执行的操作"); } }
第二:
public class Dog { public void init() { System.out.println("使用@Bean注解,的init指定bena初始化之后的代码"); } public void destroy() { System.out.println("使用@Bean注解,的destroy指定bena初始化之后的代码"); } }
@Bean(initMethod = "init",destroyMethod = "destroy") public Dog getDog() { return new Dog(); }
第三:
public class Duck { @PostConstruct public void init() { System.out.println("使用@PostConstruct注解,指定bena初始化之后的代码"); } @PreDestroy public void destroy() { System.out.println("使用@PreDestroy注解,指定bena初始化之后的代码"); } }
原文:https://www.cnblogs.com/songfahzun/p/9231671.html