Spring之在学习(2)

更多注入

基本类型即集合注入
  • 实现类
package com.wanggs.service.impl;


import com.wanggs.dao.BookDao;
import com.wanggs.service.BookService;

import java.security.PrivateKey;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * Created by wanggs on 2017/7/7.
 */
public class BookServiceImpl implements BookService{

    //之前的方式 接口 = 实现类;(多态)
    //spring: 接口+set方法

    //基本类型和集合注入
    private BookDao bookDao;
    private Integer id;
    private String name;
    private List list;
    private Set set;
    Map map;
    Properties properties;

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setList(List list) {
        this.list = list;
    }

    public void setSet(Set set) {
        this.set = set;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    public void setBookDao(BookDao bookDao ){
        this.bookDao = bookDao;
    }
    public void saveBook() {
        this.bookDao.addBook();
    }

    public BookDao getBookDao() {
        return bookDao;
    }

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public List getList() {
        return list;
    }

    public Set getSet() {
        return set;
    }

    public Map getMap() {
        return map;
    }

    public Properties getProperties() {
        return properties;
    }

    @Override
    public String toString() {
        return "BookServiceImpl{" +
                "bookDao=" + bookDao +
                ", id=" + id +
                ", name='" + name + '\'' +
                ", list=" + list +
                ", set=" + set +
                ", map=" + map +
                ", properties=" + properties +
                '}';
    }
}

  • 配置文件



    
    
    
        
    

    
    
        
        
        
        
        
            
                Rose
                Tom
            
        
        
        
            
                100
                200
            
        
        
        
            
                
                
            
        
        
        
            
                m1
                m2
            
        
    
    

  • 测试
package com.wanggs.service;

import com.wanggs.service.impl.BookServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Map;
import java.util.Set;

/**
 * Created by wanggs on 2017/7/7.
 */
public class BookServiceTest {
    public static void main(String[] args) {
        // 读取配置文件,使用spring控制反转交给spring创建对象
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        // "bookService"-->>即配置文件 (id: bean 在容器的名称)
        //用的实现类接受便于测试
        BookServiceImpl bookService = (BookServiceImpl) applicationContext.getBean("bookService");
        bookService.saveBook();
        Map map = bookService.getMap();
        Set set = map.keySet();
        for(String key : set){
            System.out.println(key+"---->"+map.get(key));

        }

        System.out.println(bookService);
    }
    /**结果:
     * DI add Book
      k1---->v1
      k2---->v2
      BookServiceImpl{bookDao=com.wanggs.dao.impl.BookDaoImpl@4dfa3a9d, id=1, name='jack', list=[Rose, Tom], set=[100, 200], map={k1=v1, k2=v2}, properties={p2=m2, p1=m1}}

     */
}


构造方法注入

  • BookService
/**
 * Created by wanggs on 2017/7/7.
 */
public class BookServiceImpl implements BookService {


    private BookDao bookDao;
    //构造方法注入
    public BookServiceImpl(BookDao bookDao) {
        this.bookDao = bookDao;
    }

    @Override
    public void saveBook() {
        this.bookDao.addBook();
    }

  • 配置文件



    
    

        
    
    
  • 测试
/**
 * Created by wanggs on 2017/7/7.
 */
public class BookServiceTest {
    public static void main(String[] args) {
        //读取配置文件,使用spring控制反转转交给spring创建对象
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        // "bookService"-->>即配置文件 (id: bean 在容器的名称)
        BookService bookService = (BookService) applicationContext.getBean("bookService");
        bookService.saveBook();

更多构造方法的注入


    
        
        
        
    
    
 

自动注入

  • ByName



    
    
    
        
    


    
    
    
    
 
  • ByType



    
    
    
        
    


    
    

    

基于注解的Bean管理

  • 配置文件


       
         
    
       
       
  • Spring内置注解
    • @Repository
    • @Component
    • @Service
代码
import java.security.PrivateKey;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * Created by wanggs on 2017/7/7.
 */

//@Repository
//@Component
@Service
public class BookServiceImpl implements BookService {


    @Autowired
    private BookDao bookDao;



    /*  //构造方法注入
        public BookServiceImpl(BookDao bookDao) {
           // System.out.println(name);
            this.bookDao = bookDao;
        }
    */
    @Override
    public void saveBook() {
        this.bookDao.addBook();
    }
}    

基于DI的Spring单元测试

添加pom依赖
  
      org.springframework
      spring-test
      4.3.9.RELEASE
    
测试
/**
 * Created by wanggs on 2017/7/7.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class BookServiceTest {
    @Autowired
    private BookServiceImpl bookService;

    @Test
    public void addBook() throws Exception {
        bookService.see();

    }

}

你可能感兴趣的:(Spring之在学习(2))