spring配置(非web程序)

spring配置:
一、非web程序(2.5.6)
1、引入jar包:spring.jar ; commons-logging-1.1.1.jar
2、配置文件:
 beans.xml(src下)


       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
          
 
 
  
  
 

3、UserService.java类:包含userDAO类,并且提供set方法

package com.bjsxt.service;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;

public class UserService {

 private UserDAO userDAO;
 
 public UserDAO getUserDAO() {
  return userDAO;
 }

 public void setUserDAO(UserDAO userDAO) {
  this.userDAO = userDAO;
 }

 public void add(User u){
  this.userDAO.save(u);
 }
}

4、spring配置很丰富:
A、
beans.xml配置如下:构造方法进行初始化

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
 
  
   
  

 


那么userService.java写法如下: 提供构造方法
package com.bjsxt.service;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;

public class UserService {
 private UserDAO userDAO;
 
 public void add(User u){
  this.userDAO.save(u);
 }
 /**
  * @param userDAO
  */
 public UserService(UserDAO userDAO){
  this.userDAO = userDAO;
 }
}

B、beans.xml配置如下:

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
 
  
 

  autowire="byType"/>

那么UserService.java的写法:UserService会根据参数类型自动装配
package com.bjsxt.service;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;

public class UserService {
 private UserDAO userDAO;
 
 public UserDAO getUserDAO() {
  return userDAO;
 }
 public void setUserDAO(UserDAO userDAO) {
  this.userDAO = userDAO;
 }
 public void add(User u){
  this.userDAO.save(u);
 }
}

C、作用范围:bean中定义scope属性,属性值为singleton等
D、初始化集合:
UserDAOImpl.java写法为:
package com.bjsxt.dao.imp;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;

public class UserDAOImpl implements UserDAO {
 private Set sets;
 private List lists;
 private Map maps;
 public Set getSets() {
  return sets;
 }
 public void setSets(Set sets) {
  this.sets = sets;
 }
 public List getLists() {
  return lists;
 }
 public void setLists(List lists) {
  this.lists = lists;
 }
 public Map getMaps() {
  return maps;
 }
 public void setMaps(Map maps) {
  this.maps = maps;
 }
 @Override
 public void save(User u) {
  System.out.println("User save.");
 }
}
那么beans.xml配置文件初始化为:

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
  
   
    1
    2
   

  

  
   
    1
    2
    3
   

  

  
   
    
    
    
   

  

 

 
  
  
  
   
  

 


E、注解@Autowired、@Qualifier使用:
beans.xml写法为:

       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">
   
 
  
 

 
 


那么userService中写法为:(自动装配u对象)
package com.bjsxt.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;

public class UserService {
 private UserDAO userDAO;
 
 public UserDAO getUserDAO() {
  return userDAO;
 }
 @Autowired
 public void setUserDAO(@Qualifier("u") UserDAO userDAO) {
  this.userDAO = userDAO;
 }
 public void add(User u){
  this.userDAO.save(u);
 }
}
F、Component使用:
beans.xml定义为:(com.bjsxt下面的类会被初始化,不用手动写这些类)

       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">
   
 
 
 


那么UserService可以写成:
package com.bjsxt.service;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;

public class UserService {
 private UserDAO userDAO;
 public UserDAO getUserDAO() {
  return userDAO;
 }
 @Resource(name="userDAOImpl")
 public void setUserDAO(UserDAO userDAO) {
  this.userDAO = userDAO;
 }
 public void add(User u){
  this.userDAO.save(u);
 }
}

UserDAOImpl需要写入@Component注解

package com.bjsxt.dao.imp;

import org.springframework.stereotype.Component;

import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;
@Component  //userDAOImpl   ;   @Component("u") @Component(value="u")   u
public class UserDAOImpl implements UserDAO {
  
 @Override
 public void save(User u) {
  System.out.println("User save.");

 }
}

G、Resource用法:
beans.xml定义为:

       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">
   
 
   
 


那么UserService.java可以写成:(Resource标签可以使用beans中定义的u对象来初始化UserService,使得UserService在beans中定义时不用写需要的参数。)
package com.bjsxt.service;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;
public class UserService {
 private UserDAO userDAO;
 public UserDAO getUserDAO() {
  return userDAO;
 }
 @Resource(name="u")
 public void setUserDAO(UserDAO userDAO) {
  this.userDAO = userDAO;
 }
 public void add(User u){
  this.userDAO.save(u);
 }
}

5、表示可以使用注解,使用某些标签时,需要引入common-annotations.jar包

6、 表示对base-package包下的类进行初始化

你可能感兴趣的:(Java)