优秀的轻量级FrameWork Spring---DI依赖注入

优秀的轻量级FrameWork Spring---DI依赖注入,第1张

 

 

public class SimpleMovieLister {

    // the SimpleMovieLister has a dependency on a MovieFinder
    private final MovieFinder movieFinder;

    // a constructor so that the Spring container can inject a MovieFinder
    public SimpleMovieLister(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }

    // business logic that actually uses the injected MovieFinder is omitted...
}

    
    
        
            administrator@example.org
            support@example.org
            development@example.org
        
    
    
    
        
            a list element followed by a reference
            
        
    
    
    
        
            
            
        
    
    
    
        
            just some string
            
        
    

    
        
    







    

重点学习这几块

  1.1 学生类 建立实体类 pojo

 

package com.kuang.pojo;

import java.util.*;

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List hobbys;
    private Map card;
    private Set games;
    private String wife;
    private Properties info;

    public String getName() {
        return name;
    }

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

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List getHobbys() {
        return hobbys;
    }

    public void setHobbys(List hobbys) {
        this.hobbys = hobbys;
    }

    public Map getCard() {
        return card;
    }

    public void setCard(Map card) {
        this.card = card;
    }

    public Set getGames() {
        return games;
    }

    public void setGames(Set games) {
        this.games = games;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Student{");
        sb.append("name='").append(name).append('\'');
        sb.append(", address=").append(address.toString());
        sb.append(", books='").append(Arrays.toString(books)).append('\'');
        sb.append(", hobbys=").append(hobbys);
        sb.append(", card=").append(card);
        sb.append(", games=").append(games);
        sb.append(", wife='").append(wife).append('\'');
        sb.append(", info=").append(info);
        sb.append('}');
        return sb.toString();
    }
}
1.2 地址类
package com.kuang.pojo;

public class Address {
    private String address;
    public String getAddress(){
        return address;
    }
    public void setAddress(String address){
        this.address=address;
    }

    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}
2.1 Spring容器 beans


    
    
        
    
    
        
        

        
        

        
        
            
                红楼梦
                西游记
                水浒传
                三国演义
            
        

        
        
            
                学习
                吃饭
                听歌
            
        

        
        
            
                
                
            
        

        
        
            
                Screeps
                lol
                炉石
            
        

        
        
            
            
        

        
        
            
                20220424
                100024
                100024
                100024
            
        
    

3.1测试

import com.kuang.pojo.Student;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void cc(){
        ClassPathXmlApplicationContext cpxac = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student)cpxac.getBean("student");
        String name = student.getName();
        System.out.println(student.toString());

        /*Student{
        name='秦疆',
        address=Address{address='北京一环爱民小区45栋303室'},
         books='[红楼梦, 西游记, 水浒传, 三国演义]',
          hobbys=[学习, 吃饭, 听歌],
           card={
           身份z=123456,
           yhk=123456
           },
           games=[Screeps, lol, 炉石],
           wife='null',
           info={
           root=100024,
           password=100024,
            url=100024,
            driver=20220424
            }
            }*/
    }
}

结果如下

 

用 setter 方式


    
    

注入不同数据类型

package kj08.pojo;
​
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
​
public class TestEntity {
    private String specialCharacter1; // 特殊字符值1
    private String specialCharacter2; // 特殊字符值2
    private Student innerBean; // JavaBean类型
    private List list; // List类型
    private String[] array; // 数组类型
    private Set set; // Set类型
    private Map map; // Map类型
    private Properties props; // Properties类型
    private String emptyValue; // 注入空字符串值
    private String nullValue = "init value"; // 注入null值
​
    public void setSpecialCharacter1(String specialCharacter1) {
        this.specialCharacter1 = specialCharacter1;
    }
​
    public void setSpecialCharacter2(String specialCharacter2) {
        this.specialCharacter2 = specialCharacter2;
    }
​
    public void setInnerBean(Student user) {
        this.innerBean = user;
    }
​
    public void setList(List list) {
        this.list = list;
    }
    
    public void setArray(String[] array) {
        this.array = array;
    }
​
    public void setSet(Set set) {
        this.set = set;
    }
​
    public void setMap(Map map) {
        this.map = map;
    }
​
    public void setProps(Properties props) {
        this.props = props;
    }
​
    public void setEmptyValue(String emptyValue) {
        this.emptyValue = emptyValue;
    }
​
    public void setNullValue(String nullValue) {
        this.nullValue = nullValue;
    }
​
    public void showValue() {
        System.out.println("特殊字符1:" + this.specialCharacter1);
        System.out.println("特殊字符2:" + this.specialCharacter2);
        System.out.println("内部Bean:" + this.innerBean.getStudentName());
        System.out.println("List属性:" + this.list);
        System.out.println("数组属性[0]:" + this.array[0]);
        System.out.println("Set属性:" + this.set);
        System.out.println("Map属性:" + this.map);
        System.out.println("Properties属性:" + this.props);
        System.out.println("注入空字符串:[" + this.emptyValue + "]");
        System.out.println("注入null值:" + this.nullValue);
    }
}
​

​
​


    
        
        
            
        
        
        
            P&G
        
        
        
            
                
                    Mr. Inner
                
            
        
        
        
            
                
                足球
                篮球
            
        
        
        
            
                
                足球
                篮球
            
        
        
        
            
                
                足球
                篮球
            
        
        
        
            
                
                
                    
                        football
                    
                    足球
                
                
                    
                        basketball
                    
                    篮球
                
            
        
        
        
            
                
                足球
                篮球
            
        
        
        
            
        
        
        
            
        
    

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/743239.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-29
下一篇 2022-04-29

发表评论

登录后才能评论

评论列表(0条)

保存