Spring——创建对象、配置及依赖注入

Spring——创建对象、配置及依赖注入,第1张

Spring——创建对象、配置及依赖注入

文章目录
  • 4.IOC创建对象的方式
  • 5.Spring配置
    • 别名
    • Bean的配置
    • import
  • DI依赖注入
    • 构造器注入
    • Set方式注入!!!!
    • 拓展方式注入
    • Bean的作用域
  • 好迷茫啊

4.IOC创建对象的方式
  1. 使用无参构造创建对象,默认!

  2. 假设我们要使用有参构造创建对象

    1. 下标赋值

      
                  
              
      
      1. 类型赋值:不建议使用
          
              
          
      
      1. 参数名
          
              
          
      

总结:在配置文件加载的时候,容器中管理的对象就已经初始化了

User

public class User {
    private String name;

    //没有无参构造,报错
    public User(String name){
        this.name = name;
    }
//省略getter,setter
    public void show(){
        System.out.println("name:"+name);
    }
}

applicationContext.xml配置















    
        
    

    

测试MyTest

import net.cqwu.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {

       //Spring容器,类似于婚介网站
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        User user = (User) context.getBean("user");
        User user2 = (User) context.getBean("user");
        user.show();
        System.out.println(user == user2);
    }
}
5.Spring配置 别名
    
Bean的配置
    
        
    
UserT user = (UserT) context.getBean("user2");
        user.show();
import

团队开发时,将多个配置文件导入合并为一个


DI依赖注入 构造器注入

    
        
        
    

    

    

Set方式注入!!!
  • 依赖注入:set注入
    • 依赖:bean对象的创建依赖于容器
    • 注入:bean对象中的所有属性,由容器来注入

【环境搭建】

  1. 复杂类型
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 + ''' +
                '}';
    }
}
  1. 真实测试对象
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;
}
//get  set  toString省略
  1. applicationContext.xml




    
        
    
    
    
    

    
        

    
        
            
                西游记
                红楼梦
                三国
                水浒
            
        

        
        
            
                魔方
                唱歌
                听歌
            
        

        
        
            
                
                
            
        

        
        
            
                lol
                bob
                coc
            
        

        
        
            
        

        
        
            
                4029
                feliks
                软件
                admin
                123456
            
        

    

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

拓展方式注入

详细解释见官方文档中的

XML Shortcut with the p-namespace
XML Shortcut with the c-namespace

对象

public class User {
    private String name;
    private int age;


    //加上无参 和 有参构造器,才能使用c命名注入
    public User() {
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }
}//省略getter setter toString

测试类

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

beans.xml




        
        

        
        


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

xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
Bean的作用域

官方文档:

ScopeDescriptionsingleton(Default) Scopes a single bean definition to a single object instance for each Spring IoC container.prototypeScopes a single bean definition to any number of object instances.requestScopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. only valid in the context of a web-aware Spring ApplicationContext.sessionScopes a single bean definition to the lifecycle of an HTTP Session. only valid in the context of a web-aware Spring ApplicationContext.applicationScopes a single bean definition to the lifecycle of a ServletContext. only valid in the context of a web-aware Spring ApplicationContext.websocketScopes a single bean definition to the lifecycle of a WebSocket. only valid in the context of a web-aware Spring ApplicationContext.
  1. 单例模式(Spring默认机制)

测试

        User user = context.getBean("user2",User.class);
        User user2 = context.getBean("user2",User.class);
        System.out.println(user==user2);//true
  1. 原型模式:每次从容器中get的时候,都会产生一个新对象

测试

        User user = context.getBean("user2",User.class);
        User user2 = context.getBean("user2",User.class);
        System.out.println(user==user2);//false

request session application只能在web开发中使用到

好迷茫啊

就当是上天都看不下去了,派了个人来让我再短暂的体验了一把温暖吧,我现在不知道该干嘛,学也学不进去,这些突如其来的人和事搞得我措手不及。看似是我在布局,实则自己变成了棋子。棋局已成,我便是弃子?

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

原文地址: http://outofmemory.cn/zaji/5138741.html

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

发表评论

登录后才能评论

评论列表(0条)

保存