【JavaSpring】依赖注入

如何使用

1.强制依赖用构造器注入,setter注入可能出现null对象

2.可选依赖用setter注入

3.Spring倡导用构造器,第三方框架都用的是构造器

4.有必要时两个可以同时用

5.如果受控对象没有提供setter方法就必须使用构造器注入

6.自己开发推荐用setter注入

第一种:setter注入(set方法)

简单类型

更改bean中文件,用value属性注入简单类型数据

package org.example.dao.impl;

import org.example.dao.BookDao;

public class BookDaoimpl implements BookDao {
    private int connextionNum;
    private String databaseName;

    public void setDatabaseName(String databaseName) {
        this.databaseName = databaseName;
    }

    public void setConnextionNum(int connextionNum) {
        this.connextionNum = connextionNum;
    }

    @Override
    public void save() {
        System.out.println("book dao save"+databaseName+","+connextionNum);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="userDao" class="org.example.dao.impl.UserDaoimpl"/>
    <bean id="bookService" class="org.example.service.impl.BookServiceimpl">
        <property name="bookDao" ref="bookDao"/>
        <property name="userDao" ref="userDao"/>
    </bean>
    <bean id="bookDao" class="org.example.dao.impl.BookDaoimpl">
        <property name="databaseName" value="mysql"></property>
        <property name="connextionNum" value="10"/>//用value属性注入简单类型数据
    </bean>
</beans>
package org.example;

import org.example.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppForDISet {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookService bookService = (BookService) ctx.getBean("bookService");
        bookService.save();
    }
}

引用类型

主要是在bean中应用

package org.example.dao.impl;

import org.example.dao.BookDao;

public class BookDaoimpl implements BookDao {
    @Override
    public void save() {
        System.out.println("book dao save");
    }
}
package org.example.dao.impl;

import org.example.dao.UserDao;

public class UserDaoimpl implements UserDao {
    @Override
    public void save() {
        System.out.println("user dao save");
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="userDao" class="org.example.dao.impl.UserDaoimpl"/>
    <bean id="bookService" class="org.example.service.impl.BookServiceimpl">
        <property name="bookDao" ref="bookDao"/>
        <property name="userDao" ref="userDao"/>
    </bean>
    <bean id="bookDao" class="org.example.dao.impl.BookDaoimpl"/>
</beans>
package org.example;

import org.example.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppForDISet {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookService bookService = (BookService) ctx.getBean("bookService");
        bookService.save();
    }
}

第二种:构造器注入(构造方法)

简单类型

与上一种一致,除了用的属性不一样

package org.example.dao.impl;

import org.example.dao.BookDao;

public class BookDaoimpl implements BookDao {
    private int connextionNum;
    private String databaseName;

    public BookDaoimpl(int connextionNum, String databaseName) {
        this.connextionNum = connextionNum;
        this.databaseName = databaseName;
    }

    @Override
    public void save() {
        System.out.println("book dao save"+databaseName+connextionNum);
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="userDao" class="org.example.dao.impl.UserDaoimpl"/>
    <bean id="bookService" class="org.example.service.impl.BookServiceimpl">
        <constructor-arg name="bookDao" ref="bookDao"/>
    </bean>
    <bean id="bookDao" class="org.example.dao.impl.BookDaoimpl">
        <constructor-arg name="connextionNum" value="10"/><constructor-arg name="databaseName" value="value"/>
    </bean>
</beans>
package org.example;

import org.example.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppForDIConstructor {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookService bookService = (BookService) ctx.getBean("bookService");
        bookService.save();
    }
}

引用类型

package org.example.service.impl;

import org.example.dao.BookDao;
import org.example.dao.impl.BookDaoimpl;
import org.example.service.BookService;

public class BookServiceimpl implements BookService {
    private BookDao bookDao;

    public BookServiceimpl(BookDaoimpl bookDao) {
        this.bookDao = bookDao;
    }

    public void save() {
        System.out.println("book service save");
        bookDao.save();
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="userDao" class="org.example.dao.impl.UserDaoimpl"/>
    <bean id="bookService" class="org.example.service.impl.BookServiceimpl">
        <constructor-arg name="bookDao" ref="bookDao"/>
    </bean>
    <bean id="bookDao" class="org.example.dao.impl.BookDaoimpl">
    </bean>
</beans>

减少耦合度

解决形参名称问题

<bean id="bookDao" class="org.example.dao.impl.BookDaoimpl">
        <constructor-arg type="int" value="10"/>
        <constructor-arg type="java.lang.String" value="value"/>
    </bean>

解决参数类型重复

<bean id="bookDao" class="org.example.dao.impl.BookDaoimpl">
        <constructor-arg index="0" value="10"/>
        <constructor-arg index="1" value="value"/>
    </bean>

特殊方法:自动装配

要保留set方法,而且必须是引用类型,而且优先级低与以上方法

按类型

保障容器中相同类型的bean唯一

<bean name="bookDao" class="org.example.dao.impl.BookDaoimpl"/>
    <bean id="bookService" class="org.example.service.impl.BookServiceimpl" autowire="byType"></bean>

按名称

保证容器中具有指定名称的bean,耦合性强

<bean name="bookDao" class="org.example.dao.impl.BookDaoimpl"/>
    <bean id="bookService" class="org.example.service.impl.BookServiceimpl" autowire="byName"></bean>

按构造方法(不推荐)

<bean name="bookDao" class="org.example.dao.impl.BookDaoimpl"/>
    <bean id="bookService" class="org.example.service.impl.BookServiceimpl" autowire="constructor"></bean>

不启用自动装配

<bean name="bookDao" class="org.example.dao.impl.BookDaoimpl"/>
    <bean id="bookService" class="org.example.service.impl.BookServiceimpl" autowire="no"></bean>

集合注入

注意:

1.array和list可以互相使用

2.还能用ref引用其他bean

package org.example.dao.impl;

import org.example.dao.BookDao;

import java.util.*;

public class BookDaoimpl implements BookDao {
    private int[] array;
    private List<String> list;
    private Set<String> set;
    private Map<String, String> map;
    private Properties properties;

    public void setArray(int[] array) {
        this.array = array;
    }

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

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

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

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

    @Override
    public void save() {
        System.out.println("book dao save");
        System.out.println("遍历数组" + Arrays.toString(array));
        System.out.println("遍历list" + list);
        System.out.println("遍历set" + set);
        System.out.println("遍历map" + map);
        System.out.println("遍历properties" + properties);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean name="bookDao" class="org.example.dao.impl.BookDaoimpl">
        <property name="array">
            <array>
                <value>100</value>
                <value>200</value>
                <value>300</value>
            </array>
        </property>
        <property name="list">
            <list>
                <value>我</value>
                <value>爱</value>
                <value>你</value>
            </list>
        </property>
        <property name="set">
            <set>
                <value>我</value>
                <value>爱</value>
                <value>你</value>
                <value>你</value>
            </set>
        </property>
        <property name="map">
            <map>
                <entry key="country" value="china"></entry>
                <entry key="province" value="jiangsu"></entry>
                <entry key="city" value="nanjing"></entry>
            </map>
        </property>
        <property name="properties">
            <props>
                <prop key="country">china</prop>
                <prop key="province">jiangsu</prop>
                <prop key="city">nanjing</prop>
            </props>
        </property>
    </bean>
</beans>
package org.example;

import org.example.dao.BookDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppForDIAtuoware{
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        BookDao bookDao=(BookDao) ctx.getBean("bookDao");
        bookDao.save();
    }
}

总结

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

岩塘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值