zoukankan      html  css  js  c++  java
  • Spring笔记(二)

    一、装配集合类型的属性:

    1.Set:

    1.<bean id="userService" class="com.szy.spring.service.UserServiceImpl">  
    2.        <property name="sets">  
    3.            <set>  
    4.                <value>Set1</value>  
    5.                <value>Set2</value>  
    6.                <value>Set3</value>  
    7.            </set>      
    8.        </property>  
    9.  
    10.</bean> 

    2.List:

    1.<bean id="userService"  class="com.szy.spring.service.UserServiceImpl">  
    2.<property name="lists">  
    3.            <list>  
    4.                <value>List1</value>  
    5.                <value>List2</value>  
    6.                <value>List3</value>  
    7.            </list>  
    8.        </property>  
    9.    </bean>  

    3.Properties:

    1.private Properties properties=new Properties();  
    2.public void setProperties(Properties properties)  
    3.    {  
    4.        this.properties = properties;  
    5.    }  
    6.public Properties getProperties()  
    7.    {  
    8.        return properties;  
    9.    }  
    10.public Properties showProperties()  
    11.    {  
    12.        return properties;  
    13.    }  
    1.  <bean id="userService" class="com.szy.spring.service.UserServiceImpl">  
    2.    <property name="properties">  
    3.            <props>  
    4.                <prop key="key1">Properties1</prop>  
    5.                <prop key="key2">Properties2</prop>  
    6.                <prop key="key3">Properties3</prop>  
    7.            </props>  
    8.        </property>  
    9.    </bean>  

    4.Map:

    1.<bean id="userService" class="com.szy.spring.service.UserServiceImpl">  
    2.  <property name="maps">  
    3.        <map>  
    4.            <entry key="key1" value="Map1"></entry>  
    5.            <entry key="key2" value="Map2"></entry>  
    6.            <entry key="key3" value="Map3"></entry>  
    7.        </map>  
    8.    </property>  
    9.</bean>  

    二、属性注入的方式:

    1.setter方法;

    2.构造器;

    3.注解。

    setter方法:使用属性setter方法注入就是给属性添加set()方法,在前面都是使用这种方法。

    构造器注入:使用构造器注入就是在类中添加含参构造函数。

    1.package com.szy.spring.service;  
    2.  
    3.import com.szy.spring.dao.PersonDao;  
    4.  
    5.public class UserServiceImplConstructor implements UserService  
    6.{  
    7.    private PersonDao personDao;  
    8.    private String name;  
    9.      
    10.    public UserServiceImplConstructor()  
    11.    {  
    12.    }  
    13.  
    14.    public UserServiceImplConstructor(PersonDao personDao, String name)  
    15.    {  
    16.        this.personDao = personDao;  
    17.        this.name = name;  
    18.    }  
    19.  
    20.    public void show()  
    21.    {  
    22.        personDao.show();  
    23.        System.out.println("name属性:"+name);  
    24.    }  
    25.}  
    1.<bean id="personDao" class="com.szy.spring.dao.PersonDaoBean"/>  
    2.    <!-- 使用构造器参数方法注入配置 -->  
    3.    <bean id="userService2" class="com.szy.spring.service.UserServiceImplConstructor">  
    4.        <constructor-arg index="0" type="com.szy.spring.dao.PersonDao" ref="personDao"/>  
    5.        <constructor-arg index="1" value="Kuka"/>  
    6.    </bean> 
    constructor-arg index从0开始。

    注解方式注入:
    1.使用javax.annotation.Resource中提供的注解方式方法
    注意添加配置信息:<context:annotation-config/>

    1.package com.szy.spring.service;  
    2.  
    3.import javax.annotation.Resource;  
    4.  
    5.import com.szy.spring.dao.PersonDao;  
    6.  
    7.public class UserServiceImplByAnnotation4Resource implements UserService  
    8.{  
    9.    //@Resource默认是按照名称装配,找不到与名称匹配的bean时按类型装配  
    10.    @Resource(name="personDao")private PersonDao personDao;  
    11.  
    12.    public void show()  
    13.    {  
    14.        personDao.show();  
    15.    }  
    16.//  下面方法同样可以  
    17.//  @Resource  
    18.//  public void setPersonDao(PersonDao personDao)  
    19.//  {  
    20.//      this.personDao = personDao;  
    21.//  }  
    22.      
    23.}
    
    
    1   <context:annotation-config/>  
    2     <bean id="personDao" class="com.szy.spring.dao.PersonDaoBean"/>  
    3     <bean id="userService" class="com.szy.spring.service.UserServiceImplByAnnotation4Autowired">  
    4    </bean>  

    2.第二中方式就是使用spring提供的注解方式

    注入使用时需要导入common-annotations.jar这个包。

    1.package com.szy.spring.service;  
    2.  
    3.import org.springframework.beans.factory.annotation.Autowired;  
    4.import org.springframework.beans.factory.annotation.Qualifier;  
    5.  
    6.import com.szy.spring.dao.PersonDao;  
    7.  
    8.public class UserServiceImplByAnnotation4Autowired implements UserService  
    9.{  
    10.    //@Autowired默认使用类型进行装配,  
    11.    @Autowired private PersonDao personDao;  
    12.//  如果使用按名称进行装配,则需要如下  
    13.//  @Autowired @Qualifier("personDao")private PersonDao personDao;  
    14.    public void show()  
    15.    {  
    16.        personDao.show();  
    17.    }  
    18.      
    19.}  

    三、让Spring自动扫描和管理Bean:

    Spring2.5为我们引入了组件自动扫描机制,它可以在类路径下寻找标记了@Component@Service@Controller@Repository注解的类,

    并把这些类纳入到spring容器中管理,它的作用和在xml中使用bean节点配置组件一样。

    <context:component-scan base-package="com.imooc"></context:component-scan> 
    base-package为需要扫描的包。
    @Service用于标注业务层的组件,@Controller用于标注控制层组件(如struts中的action),@Repository用于标注数据访问组件,即DAO组件,而@Component泛指组件,
    当组件不好归类的时候,我们可以使用这个注解进行标注。

    1 package com.imooc.myinterface;
    2 
    3 public interface UserDao  {
    4     void show();
    5 }
    
    
     1 package com.imooc.impl;
     2 
     3 import com.imooc.myinterface.UserDao;
     4 import org.springframework.stereotype.Repository;
     5 
     6 @Repository
     7 public class UserDao4MysqlImpl implements UserDao {
     8     @Override
     9     public void show() {
    10         System.out.println("UserDao4MysqlImpl");
    11     }
    12 }
     1 package com.imooc.impl;
     2 
     3 import com.imooc.myinterface.UserDao;
     4 import com.imooc.myinterface.UserService;
     5 import org.springframework.stereotype.Service;
     6 
     7 import java.util.HashSet;
     8 import java.util.Set;
     9 
    10 @Service("userServiceImpl")
    11 public class UserServiceImpl implements UserService {
    12 
    13     private UserDao userDao;
    14 
    15     @Override
    16     public void show() {
    17         userDao.show();
    18     }
    19 
    20     public void setUserDao(UserDao userDao) {
    21         this.userDao = userDao;
    22     }
    23 
    24 
    25 }
    1 @Test
    2     public void testUserService(){
    3         ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    4         UserService userService = (UserService) context.getBean("userServiceImpl");
    5         System.out.println(userService);
    6 
    7     }

    结果:

     1 com.imooc.impl.UserServiceImpl@1311cccd 

    在Spring默认的id是类的名称,开头小写;也可自己命名@Service(“名字”);

    若每次调用都想产生一个实例@Service("名字") @Scope("prototype")

    配置初始化、销毁方法:@PostConstruct、@PreDestory




  • 相关阅读:
    Bean复制
    java中的ConcurrentModificationException异常
    线程安全问题
    多线程等待唤醒机制之生产消费者模式
    JavaScript数据结构——队列的实现
    JavaScript数据结构——链表的实现
    JavaScript数据结构——栈的实现
    java中map集合的迭代
    SQLServer查询最近一天,三天,一周,一月,一季度方法
    细数网络上十七种安全威胁
  • 原文地址:https://www.cnblogs.com/zhongzhongdebokeyuan/p/5820939.html
Copyright © 2011-2022 走看看