栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring IOC 手动注入

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring IOC 手动注入

Spring IOC 注入 主动实例化

1.设置配置文件




    
    

2.编写Bean对象
AccountDao.java

package com.xxxx.dao;

public class AccountDao {

    public void test(){
        System.out.println("AccountDao...");
    }
}

AccountService.java

package com.xxxx.service;

import com.xxxx.dao.AccountDao;

public class AccountService {
    //主动实例化
    AccountDao accountDao = new AccountDao();

    public void test(){
        System.out.println("AccountService...");
        accountDao.test();
    }
}

3.获取实例化对象

package com.xxxx;



import com.xxxx.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");

        AccountService accountService = (AccountService) ac.getBean("accountService");

        accountService.test();

    }
}

4.运行结果

AccountService...
AccountDao...
Spring IOC 手动注入(装配) set方法注入

属性字段需要提供set⽅法

业务对象 JavaBean

1.dao层的bean对象

package com.xxxx.dao;

public class AccountDao {

    public void test(){
        System.out.println("AccountDao...");
    }
}

2.属性字段提供set方法

package com.xxxx.service;

import com.xxxx.dao.AccountDao;

public class AccountService {

    //JavaBean对象 手动注入 set方法注入
    private AccountDao accountDao;
    //提供set方法
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public void test(){
        System.out.println("AccountService...");

        accountDao.test();
    }
}

3.配置文件的bean标签设置property标签




    
    
    
    
    
    
        
    


4.获取实例化对象

package com.xxxx;



import com.xxxx.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");

        AccountService accountService = (AccountService) ac.getBean("accountService");

        accountService.test();

    }
}

5.运行结果

AccountService...
AccountDao...
常用对象和基本类型

1.属性字段提供set方法

package com.xxxx.service;

public class AccountService {

    //字符串类型
    private String host;
    //提供set方法
    public void setHost(String host) {
        this.host = host;
    }

    //基本类型
    private Integer port;
    //提供set方法
    public void setPort(Integer port) {
        this.port = port;
    }

    public void test(){
        System.out.println("AccountService...");
        System.out.println(host);
        System.out.println(port);
    }
}

2.配置文件的bean标签设置property标签




    
    
    
    
        
        
        
        
    


3.获取实例化对象

package com.xxxx;



import com.xxxx.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");

        AccountService accountService = (AccountService) ac.getBean("accountService");

        accountService.test();

    }
}

4.运行结果

AccountService...
localhost
8080
集合类型和属性对象

1.属性字段提供set方法

package com.xxxx.service;

import com.xxxx.dao.AccountDao;

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

public class AccountService {

    //List集合
    private List list;

    public void setList(List list) {
        this.list = list;
    }
    //list集合输出
    public void printList(){
        list.forEach(v -> System.out.println(v));
    }


    //Set集合
    private Set set;

    public void setSet(Set set) {
        this.set = set;
    }
    // Set集合输出
    public void printSet() {
        set.forEach(s -> System.out.println(s));
    }


    //Map
    private Map map;

    public void setMap(Map map) {
        this.map = map;
    }
    // Map输出
    public void printMap() {
        map.forEach((k,v) -> System.out.println(k + "," + v));
    }


    //Properties
    private Properties properties;

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

    // Properties输出
    public void printProperties(){
        properties.forEach((k,v) -> System.out.println(k + ","+ v ));
    }



    public void test(){
        System.out.println("AccountService...");
        // List集合
        printList();
        // Set集合
        printSet();
        // Map
        printMap();
        // Properties
        printProperties();
    }

}

2.配置文件的bean标签设置property标签





    
    
    
    
    

        
        
            
                北京
                上海
                深圳
            
        

        
        
            
                上海SH
                北京BJ
                杭州HZ
            
        


        
        
            
                
                    周杰伦
                    我是如此相信
                
                
                    林俊杰
                    可惜没如果
                
                
                    陈奕迅
                    ⼗年
                
            
        


        
        
            
                东⽅明珠
                天安⻔
                ⻄湖
            
        

    


3.获取实例化对象

package com.xxxx;



import com.xxxx.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");

        AccountService accountService = (AccountService) ac.getBean("accountService");

        accountService.test();

    }
}

4.运行结果

AccountService...
北京
上海
深圳
上海SH
北京BJ
杭州HZ
周杰伦,我是如此相信
林俊杰,可惜没如果
陈奕迅,⼗年
上海,东⽅明珠
北京,天安⻔
杭州,⻄湖
构造器注入

需要提供带参构造器,构造器有几个参数就需要设置几个constructor-arg标签
通过constructor-arg标签设置构造器的形参(name 形参名 )

单个Bean对象作为参数

1.JavaBean对象

package com.xxxx.service;

import com.xxxx.dao.TypeDao;


public class TypeService {

    private TypeDao typeDao;

    //提供带参构造
    public TypeService(TypeDao typeDao) {
        this.typeDao = typeDao;
    }

    public void test(){
        System.out.println("TypeService...");
        typeDao.test();
    }
}

2.设置配置文件




    
    
    
        
    
    

3.获取实例化对象

package com.xxxx;

import com.xxxx.service.TypeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");

        TypeService typeService = (TypeService) ac.getBean("typeService");

        typeService.test();

    }
}

4.运行结果

TypeService...
TypeDao...
多个Bean对象作为参数

1.JavaBean对象

package com.xxxx.service;

import com.xxxx.dao.TypeDao;
import com.xxxx.dao.UserDao;


public class TypeService {

    private TypeDao typeDao;
    private UserDao userDao;

    //提供带参构造
    public TypeService(TypeDao typeDao, UserDao userDao) {
        this.typeDao = typeDao;
        this.userDao = userDao;
    }

    public void test(){
        System.out.println("TypeService...");
        typeDao.test();
        userDao.test();
    }
}

2.设置配置文件




    
    
    
    
        
        
    


3.获取实例化对象

package com.xxxx;

import com.xxxx.service.TypeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");

        TypeService typeService = (TypeService) ac.getBean("typeService");

        typeService.test();

    }
}

4.运行结果

TypeService...
TypeDao...
UserDao...
Bean对象和常用对象作为参数
package com.xxxx.service;

import com.xxxx.dao.TypeDao;
import com.xxxx.dao.UserDao;


public class TypeService {

    private TypeDao typeDao;
    private UserDao userDao;
    private String name;

    public TypeService(TypeDao typeDao, UserDao userDao, String name) {
        this.typeDao = typeDao;
        this.userDao = userDao;
        this.name = name;
    }

    public void test(){
        System.out.println("TypeService...");
        typeDao.test();
        userDao.test();
        System.out.println(name);
    }
}

设置配置文件




    
    
    
    
        
        
        
        
    


获取实例化对象

package com.xxxx;

import com.xxxx.service.TypeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");

        TypeService typeService = (TypeService) ac.getBean("typeService");

        typeService.test();

    }
}

运行结果

TypeService...
TypeDao...
UserDao...
admin
循环依赖问题

循环问题产生的原因:
Bean通过构造器注入,之间彼此相互依赖对方导致bean无法实例化。

问题展示:

A.java

package com.xxxx.service;

import com.xxxx.dao.B;

public class A {

    private B b;

    public A(B b) {
        this.b = b;
    }

    public void test(){
        System.out.println("A test ...");
        b.test();
    }
}

B.java

package com.xxxx.dao;

import com.xxxx.service.A;

public class B {
    private A a;

    public B(A a) {
        this.a = a;
    }

    public void test(){
        System.out.println("B test ...");
        a.test();
    }
}

设置配置文件




    
        
    
    
        
    
    

测试

package com.xxxx;

import com.xxxx.service.TypeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
    }
}
解决循环依赖

如何解决:将构造器注入改为set方法注入

1.将构造方法换成set方法

package com.xxxx.service;

import com.xxxx.dao.B;

public class A {

    private B b;

//    public A(B b) {
//        this.b = b;
//    }


    public void setB(B b) {
        this.b = b;
    }

    public void test(){
        System.out.println("A test ...");
        b.test();
    }
}

package com.xxxx.dao;

import com.xxxx.service.A;

public class B {
    private A a;

//    public B(A a) {
//        this.a = a;
//    }


    public void setA(A a) {
        this.a = a;
    }

    public void test(){
        System.out.println("B test ...");
        a.test();
    }
}

2.修改标签












    
        
    
    
        
    


静态工厂注入

1.定义静态工厂类

package com.xxxx.factory;


import com.xxxx.dao.UserDao;



public class StaticFactory {
    
    public static UserDao createUserDao(){
        System.out.println("静态工厂实例化...");
        return new UserDao();
    }

}

2.java代码

package com.xxxx.service;

import com.xxxx.dao.UserDao;

public class UserService {


    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void test(){
        System.out.println("UserService...");
    }
}

xml配置

    
        
    

    
实例化工厂注入

1.定义工厂类

package com.xxxx.factory;

import com.xxxx.dao.UserDao;

public class InstanceFactory {
    
    public UserDao createUserDao(){
        System.out.println("实例化工厂实例化...");
        return new UserDao();
    }
}

2.java代码

package com.xxxx.service;

import com.xxxx.dao.UserDao;

public class UserService {


    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void test(){
        System.out.println("UserService...");
    }
}

3.xml配置

    
        
    
    
    
    
注入方式的选择

开发项目中set方式注入首选

	使⽤构造注入可以在构建对象的同时⼀并完成依赖关系的建⽴,对象⼀建⽴则所有的⼀切也就准备好了,但如果要建⽴的对象关系很多,
使⽤构造器注⼊会在构建函数上留下⼀⻓串的参数,且不易记忆,这时使⽤Set注⼊会是个不错的选择。

  使⽤Set注⼊可以有明确的名称,可以了解注⼊的对象会是什么,像setXXX()这样的名称会⽐记忆Constructor上某个参数的位置代表某个对象更好。
p名称空间的使用

spring2.5以后,为了简化setter方法属性注入,引用p名称空间的概念,可以将 property 子元素简化为 bean 元素属性配置。

1.属性字段提供 set 方法

public class UserService {
	 // 业务对象UserDao set注⼊(提供set⽅法)
	 private UserDao userDao;
	 public void setUserDao(UserDao userDao) {
		 this.userDao = userDao;
	 }
	 
	 // 常⽤对象String set注⼊(提供set⽅法)
	 private String host;
	 public void setHost(String host) {
		 this.host = host;
	 }
}

2.在配置文件spring.xml引入p名称空间

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

 
 
 
 
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/396661.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号