Spring笔记3

1.    使用Field注入,注解方式

  1)加入命名空间,第4,7,8,9行 

1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4        xmlns:context="http://www.springframework.org/schema/context"
5        xsi:schemaLocation="http://www.springframework.org/schema/beans
6            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
7            http://www.springframework.org/schema/context 
8            http://www.springframework.org/schema/context/spring-context-3.0.xsd">
9            <context:annotation-config></context:annotation-config>

  2)加入jar文件 lib\j2ee\common-annotations.jar

  3)在java代码中使用@Autowired和@Resource注解方式进行装配,这两个注解的区别是@Autowired默认按类型装配,@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配,都可用于字段上也可用于set方法上。

  @Autowired默认情况下要求依赖对象必须存在,如果允许为空值可以设置它的requried属性为false,如果想使用按名称装配可以结合@Qualifier注解一起使用,如:@Autowired(required=false) @Qualifier("personDaobean").@Resource默认按名称装配,名称可以通过name属性设定,如果没有指定name属性,则默认按照属性名称作为bean名称寻找依赖对象,如果还是找不到,会按类型装配,一旦设定了name值就只能按名称装配了。

  4)例子

bean配置 
<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
           <context:annotation-config></context:annotation-config>
    <bean id="personDao" class="cn.itcast.dao.imp.PersonDaoImp"></bean>
    <bean id="personService" class="cn.itcast.service.imp.PersonServiceBean">
    </bean>
</beans>
service的代码 
package cn.itcast.service.imp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.annotation.Resource;

import cn.itcast.dao.PersonDao;
import cn.itcast.service.PersonService;

public class PersonServiceBean implements PersonService {
    
    @Resource private PersonDao personDao;
    
    public PersonServiceBean(){
        
    }
    public void save(){
        personDao.save();
    }
    
}

也可以用在set方法上

  private PersonDao personDao;
    
    @Resource
    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }

 2.  关于自动装配了解即可,不推荐使用

    <bean id=""  class=""  autowire="byType"></bean>

     autowire属性取值如下:

      (1)byType:按类型装配,可以根据属性的类型,在容器中寻找跟该类型匹配的bean,如果发现多个会抛出异常,如果没找到,则属性值为null。

      (2)byName:按名称装配,可以根据属性的名称,在容器中寻找跟该熟悉名相同的bean,如果没有找到则属性值为null。

      (3)constructor与byType类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的bean,将会抛出异常。

      (4)autodetect通过bean类的自省(introspection)机制来决定是使用byType还是contructor方式进行自动装配。如果发现默认的构造器将使用byType方式进行。

3.  通过在classpath自动扫描方式把组件纳入Spring容器中管理。使用xml配置的bean定义来配置组件,在大项目中会增加配置文件的体积,查找和维护不太方便。Spring2.5为我们引入了组件自动扫描机制,它可以再路径下寻找标注了@Component、@Service、@Controller、@Repository注解的类,并把这些类纳入Spring容器中管理,它的作用和在xml中配置bean节点是一样的,要使用自动扫描机制要打开以下配置信息:

  <context:component-scan base-package="it.cast"></context:component-scan>(base-package为需要扫描的包,含子包)

  @Service用于标注业务层组件、@Controller用于标注控制层组件、@Repository用于标注数据访问组件,即dao组件,而@Component泛指组件,当组件不好归类的时候可以用这个注解标注

  默认bean名称为类名的第一个字母小写,也可以指定,如@Service("personBean"),也可以指定非单例的模式,如@Service("personBean") @Scope("prototype"),也可以指定初始化方法@PostConstruct,摧毁方法@PreDestroy

    

bean
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 7            http://www.springframework.org/schema/context 
 8            http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 9     <context:component-scan base-package="cn.itcast"></context:component-scan>
10 </beans>
service
 1 package cn.itcast.service.imp;
 2 
 3 
 4 import org.springframework.context.annotation.Scope;
 5 import org.springframework.stereotype.Service;
 6 
 7 import cn.itcast.dao.PersonDao;
 8 import cn.itcast.service.PersonService;
 9 
10 @Service("personService") @Scope("prototype")
11 public class PersonServiceBean implements PersonService {
12     
13     private PersonDao personDao;
14     
15     String name;
16     
17     public void setPersonDao(PersonDao personDao) {
18         this.personDao = personDao;
19     }
20 
21     public void save(){
22         personDao.save();
23     }
24     
25 }
test
 1 package junit.test;
 2 
 3 import java.util.Set;
 4 
 5 import org.junit.Test;
 6 import org.springframework.context.ApplicationContext;
 7 import org.springframework.context.support.AbstractApplicationContext;
 8 import org.springframework.context.support.ClassPathXmlApplicationContext;
 9 import org.springframework.context.support.FileSystemXmlApplicationContext;
10 
11 import cn.itcast.service.PersonService;
12 
13 import junit.framework.TestCase;
14 
15 public class SpringTest extends TestCase {
16 
17     @Test
18     public void test1(){
19         AbstractApplicationContext ctr = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
20         PersonService personService = (PersonService) ctr.getBean("personService");
21         PersonService personService1 = (PersonService) ctr.getBean("personService");
22         System.out.println(personService==personService1);
23     }
24 }
原文地址:https://www.cnblogs.com/fanglove/p/2813361.html