Java全栈课程之Spring详解——依赖注入

一、构造器注入

二、set方式注入

        1.依赖注入:set注入

        2.依赖:bean对象的创建依赖于容器

        3.注入;bean对象中的所有属性,有容器来注入

        4.环境搭建

                ① 复杂类型

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

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

                ② 真实对象

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List hobbys;
    private Map card;
    private Set game;
    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 getGame() {
        return game;
    }

    public void setGame(Set game) {
        this.game = game;
    }

    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() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", game=" + game +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}

                ③ beans.xml



    
	  
        
    

                ④ 测试类

public class MyText {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans. xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.getName());
    }
}

                ⑤ 完善注解



    
    
        
        
        
        
        
        
            
                java
                c
                python
                c++
            
        
        
        
            
                听歌
                刷抖音
                敲代码
            
        
        
        
            
                
                
            
        
        
        
            
                王者
                吃鸡
                蛋仔
            
        
        
        
            /
        
        
        
            
                20240205
                
                小明
                123456
            
        
    

三、其他方式注入

        我们可以使用p命名空间和c命名空间进行注入

官方解释:

Java全栈课程之Spring详解——依赖注入_第1张图片

 使用 :



    
    
    
    

测试:

@Test
public void text2(){
    ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
    User user = context.getBean("user", User.class);
    System.out.println(user);
}
@Test
public void text3(){
    ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
    User user = context.getBean("user2", User.class);
    System.out.println(user);
}

注意:p命名和c命名空间不能直接使用,需要导入xml约束

xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"

四、bean的作用域

Scope

Description

singleton

(Default) Scopes a single bean definition to a single object instance for each Spring loC container.

prototype

Scopes a single bean definition to any number of object instances.

request

Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a beancreated off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.

session

Scopes a single bean definition to the lifecycle of an HTTP Session . Only valid in the context of a web-aware Spring ApplicationContext.

application

Scopes a single bean definition to the lifecycle of a ServletContext . Only valid in the context of a web-aware Spring ApplicationContext.

websocket

Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.

 1.单例模式(spring默认机制)

2.原型模式:每次从容器中get的时候,都会产生一个新对象

3.其余的request、session、application,这些个只能在web开发中使用到 

你可能感兴趣的:(Java全栈开发,spring,java,后端)