zoukankan      html  css  js  c++  java
  • spring之bean学习笔记

      整理学习的笔记 加深理解 精彩的人生需要一步一步的积累 加油   

    本文主要是学习spring容器bean的创建、bean的作用域、依赖注入、懒加载、初始化方法与销毁方法

    详细的解释都在代码里面了 我就不多说了

       测试代码SpringTest.java

    package com.undergrowth.test;
    
    import org.junit.BeforeClass;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.undergrowth.bean.service.IPersonDao;
    import com.undergrowth.bean.serviceimple.PersonDao;
    
    /*
     * 这个测试用例主要是学习spring容器bean的创建、bean的作用域、依赖注入、懒加载、初始化方法与销毁方法
     */
    public class SpringTest {
    
    	private static  AbstractApplicationContext ac;
    	private  IPersonDao ipd;
    	//在进行测试之前先进行spring的初始化
    	@BeforeClass
    	public  static void beforeTest(){
    		ac=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","di.xml","scope.xml"});
    	}
    	
    	//测试自动扫描功能
    	@Test
    	public void test() {
    		//获取通过自动扫描进行注册的bean 
    		//如果是自动扫描注册的bean的话 其bean的id标示符应该是第一个字母小写 后面的单词就是驼峰法的命名规则
    		ipd=ac.getBean("personDao",PersonDao.class);
    		ipd.sayWhat("你好 spring");
    		
    	}
    	
    	//测试使用构造器创建的bean
    	@Test
    	public void test2() {
    		ipd=ac.getBean("personDao2",PersonDao.class);
    		ipd.sayWhat("你好 spring,我是使用构造器创建的bean");
    		
    	}
    	
    	//测试使用静态工厂创建的bean
    	@Test
    	public void test3() {
    		ipd=ac.getBean("personDao3",PersonDao.class);
    		ipd.sayWhat("你好 spring,我是使用静态工厂方法创建的bean");
    		
    	}
    
    	//测试使用构造器和setter进行注入
    	@Test
    	public void test4() {
    		ipd=ac.getBean("personDao4",PersonDao.class);
    		ipd.sayWhat("你好 spring,我是使用构造器和setter进行注入");
    		
    	}
    	//用于测试bean的作用范围
    	@Test
    	public void test56() {
    		ipd=ac.getBean("personDao6",PersonDao.class);
    		ipd=ac.getBean("personDao6",PersonDao.class);
    		
    	}
    	
    	/*测试bean的初始化与销毁*/
    	@Test
    	public void test7() {
    		ipd=ac.getBean("personDao7",PersonDao.class);
    		
    		ac.close();
    	}
    }
    


    配置文件

    applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    	xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:p="http://www.springframework.org/schema/p"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    	http://www.springframework.org/schema/context
    	http://www.springframework.org/schema/context/spring-context-3.1.xsd"
    	>
    	<!-- 打开自动扫描注册类的功能 
    	context:component-scan
    	自动的将类中使用 @Controller @Service @Repository @Component 进行注解的类 加入到spring的容器中进行管理
    	并且还隐式的将 AutowiredAnnotationBeanPostProcessor and CommonAnnotationBeanPostProcessor进行了注册
    	-->
        <context:component-scan base-package="com.undergrowth.bean">
        </context:component-scan>
       
       <!-- 实例化bean共有三种方法 分别为使用构造器 使用静态工厂方法 使用实例工厂方法 -->
      <!--  第一种 使用构造器 现在这种是使用无参数的构造器进行创建bean -->
       <bean id="personDao2" class="com.undergrowth.bean.serviceimple.PersonDao"></bean>
        <!-- 第二种 使用静态工厂方法 -->
       <bean id="personDao3" class="com.undergrowth.utils.UnderBeanFactory" factory-method="createInstance"></bean>
    	
    	
    	
    </beans>

    scope.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    	xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:p="http://www.springframework.org/schema/p"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
    	>
    	
    	<!--
    	 上面的PersonDao中的person属性 是通过Autowired进行依赖注入的 现在使用构造器参数和setter进行注入
    	 并且加入了bean的范围测试
    	  -->
    	<bean id="person5" class="com.undergrowth.bean.Person">
    	 <property name="name" value="qq"></property>
    	 <property name="age" value="208"></property>
    	</bean>
    	<!-- 默认bean的范围为singleton singleton表示在spring容器中只会有一个id为personDao5的实例 -->
    	<!-- 默认情况下,spring容器在初始化的时候会加载和配置scope为singleton的bean 
    	但是可以使用lazy-init属性 设置其在使用getBean的时候进行加载 -->
    	<bean id="personDao5" class="com.undergrowth.bean.serviceimple.PersonDao" scope="singleton" lazy-init="true">
    	 <constructor-arg index="0" value="你想说什么,bean的作用域测试"></constructor-arg>
    	 <constructor-arg index="1" ref="person5"></constructor-arg>
    	</bean>
    	<!-- prototype表示在每一次获取bean的时候 都创建一个新的bean的实例
    	 prototype在spring容器初始化的时候不会加载容器
    	  在使用getBean的时候进行加载 
    	  -->
    	 
    	<bean id="personDao6" class="com.undergrowth.bean.serviceimple.PersonDao" scope="prototype"
    	>
    	 <constructor-arg index="0" value="你想说什么,bean的作用域测试"></constructor-arg>
    	 <constructor-arg index="1" ref="person5"></constructor-arg>
    	</bean>
    	
    	 <!-- 在创建容器的时候可以加入初始化方法和销毁方法 
    	  init-method-进行初始化工作
    	  destroy-method-进行扫尾工作(AbstractApplicationContext才有close方法)
    	  -->
    	<bean id="personDao7" class="com.undergrowth.bean.serviceimple.PersonDao" 
    	init-method="init" destroy-method="close"></bean>
    	
    </beans>


    di.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    	xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:p="http://www.springframework.org/schema/p"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
    	>
    	
    	<!--
    	 上面的PersonDao中的person属性 是通过Autowired进行依赖注入的 现在使用构造器参数和setter进行注入 -->
    	<bean id="person4" class="com.undergrowth.bean.Person">
    	 <property name="name" value="undergrowth"></property>
    	 <property name="age" value="108"></property>
    	</bean>
    	<bean id="personDao4" class="com.undergrowth.bean.serviceimple.PersonDao">
    	 <constructor-arg index="0" value="你想说什么"></constructor-arg>
    	 <constructor-arg index="1" ref="person4"></constructor-arg>
    	</bean>
    	
    	
    	
    </beans>


    服务层代码

    PersonDao.java

    package com.undergrowth.bean.serviceimple;
    
    import java.util.Date;
    
    import org.aspectj.weaver.NewConstructorTypeMunger;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.undergrowth.bean.Person;
    import com.undergrowth.bean.service.IPersonDao;
    
    //告知spring的容器 将该类加入到spring的容器中进行管理
    @Service
    public class PersonDao implements IPersonDao {
    	
    	private String what;
    	//按照类型进行依赖注入person
    	@Autowired
    	private Person person;
    	public String getWhat() {
    		return what;
    	}
    	public void setWhat(String what) {
    		this.what = what;
    	}
    	public Person getPerson() {
    		return person;
    	}
    	public void setPerson(Person person) {
    		this.person = person;
    	}
    	public PersonDao(String what, Person person) {
    		super();
    		this.what = what;
    		this.person = person;
    		System.out.println(new Date().getTime());
    	}
    	public PersonDao() {
    		super();
    	}
    	/* (non-Javadoc)
    	 * @see com.undergrowth.bean.serviceimple.IPersonDao#sayWhat()
    	 */
    	@Override
    	public void sayWhat(String what)
    	{
    		System.out.println("you say what:"+what+"	"+this.what+"	"+person);
    	}
    	
    	public void init()
    	{
    		System.out.println("我在开始初始化工作");
    	}
    	
    	public void close()
    	{
    		System.out.println("我在开始扫尾工作");
    	}
    	
    }
    

    持久层代码

    Person.java

    package com.undergrowth.bean;
    
    import org.springframework.stereotype.Repository;
    
    //告知spring的容器 将该类加入到spring的容器中进行管理
    @Repository
    public class Person {
    	private String name;
    	private Integer age;
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public Integer getAge() {
    		return age;
    	}
    	public void setAge(Integer age) {
    		this.age = age;
    	}
    	@Override
    	public String toString() {
    		return "Person [name=" + name + ", age=" + age + "]";
    	}
    	public Person() {
    		super();
    		//为了测试数据的方法  加入测试数据
    		name="张三";
    		age=20;
    	}
    	public Person(String name, Integer age) {
    		super();
    		this.name = name;
    		this.age = age;
    	}
    	
    }
    

    用于静态工厂创建bean的类  UnderBeanFactory.java

    package com.undergrowth.utils;
    
    import com.undergrowth.bean.serviceimple.PersonDao;
    
    
    /*
     * 用于解释使用静态工厂方法创建的bean
     */
    public class UnderBeanFactory {
    	
    	public static Object createInstance()
    	{
    		return new PersonDao();
    	}
    }
    


  • 相关阅读:
    Redis 安装和启动
    有序的map类--LinkedHashMap
    Springmvc
    工厂模式
    Top K
    判断链表是否有环
    注解
    Spring 事务
    热点账户高并发解决方案
    sql优化
  • 原文地址:https://www.cnblogs.com/liangxinzhi/p/4275585.html
Copyright © 2011-2022 走看看