Spring注入和注解实现IOC

注入

依赖注入的方式

在使用依赖注入时,如果注入的是 Bean 对象,那么要求注入的 Bean 对象与被注入的
Bean 对象都需要 Spring

通过Set方法注入

需要为注入的成员变量提供 Set 方法。

  1. 配置文件
<bean id="usersDao" class="com.bjsxt.dao.impl.UsersDaoImpl"/>
<bean id="usersDaoMybatis" class="com.bjsxt.dao.impl.UsersDaoImpl"/>
<bean id="usersService" name="name1,name2,name3" class="com.bjsxt.service.impl.UsersServiceImpl">
	<!--<name要和UsersServiceImpl中属性名相同-->
	<property name="usersDao">
		<ref bean="usersDaoMybatis"/>
	</property>
<!--<property name="usersDao" ref="usersDaoMybatis"/>-->
</bean>
  1. Bean对象
private UsersDao usersDao;
public UsersDao getUsersDao() {
	return usersDao;
}
public void setUsersDao(UsersDao usersDao) {
	this.usersDao = usersDao;
}

通过构造方法注入

Bean 对象中需要提供有参的构造方法

  1. 配置文件
<bean id="usersDaoMybatis" class="com.bjsxt.dao.impl.UsersDaoMybatisImpl"/>
<bean id="usersService" name="name1, name2, name3" class="com.bjsxt.service.impl.UsersServiceImpl">
    <!--<property name="usersDao" ref="usersDaoMybatis"/>-->
        <!--一个constructor-arg标签表示构造方法中的一个参数
        name:根据参数名称识别参数
        index:根据参数的位置识别参数
        type:根据参数类型识别参数-->
        <constructor-arg type="com.bjsxt.dao.UsersDao">
            <ref bean="usersDaoMybatis"/>
        </constructor-arg>
</bean>

2.Bean对象

private UsersDao usersDao;
public UsersServiceImpl(UsersDao usersDao){
	this.usersDao = usersDao;
}

自动注入

自动注入的方式有两种,一种是全局配置自动注入,另一种是局部配置自动注入。
无论全局配置或局部单独配置,都有 5 个值可以选择:

  • no:当 autowire 设置为 no 的时候,Spring 就不会进行自动注入。
  • byName:在 Spring 容器中查找 id 与属性名相同的 bean,并进行注入。需要提供 set 方
    法。
  • byType:在 Spring 容器中查找类型与属性名的类型相同的 bean,并进行注入。需要提
    供 set 方法。
  • constructor:仍旧是使用 byName 方式,只不过注入的时候,使用构造方式进行注入。
  • default:全局配置的 default 相当于 no,局部的 default 表示使用全局配置设置

局部自动注入:通过 bean 标签中的 autowier 属性配置自动注入,有效范围:仅针对当前 bean 标签生效
全局自动注入:通过 beans 标签中的 default-autowire , 有效范围:配置文件中的所有 bean

依赖注入的数据类型

注入Bean对象

方式一:

<property name="FieldName">
	<ref bean="BeanID"/>
</property>

方式二:

<property name="FieldName" ref="BeanID"/>

注入基本数据类型和字符串

方式一:

<property name="FieldName">
	<value>content</value>
</property>
<!--content是要传入内容的值-->

方式二

<property name="FieldName" value="Content"/>

注入List

<property name="users">
            <list>
                <bean class="com.bjsxt.pojo.Users">
                    <property name="username" value="Oldlu"/>
                    <property name="userage" value="30"/>
                </bean>
                <bean class="com.bjsxt.pojo.Users">
                    <property name="username" value="admin"/>
                    <property name="userage" value="20"/>
                </bean>
            </list>
        </property>

注入Set

<property name="usersSet">
            <set>
                <bean class="com.bjsxt.pojo.Users">
                    <property name="username" value="Oldlu-set"/>
                    <property name="userage" value="30"/>
                </bean>
                <bean class="com.bjsxt.pojo.Users">
                    <property name="username" value="admin-set"/>
                    <property name="userage" value="20"/>
                </bean>
            </set>
        </property>

注入Map

方式一:

 <property name="map">
            <map>
                <entry key="key1" value="value1"/>
                <entry key="key2" value="value2"/>
            </map>
 </property>

方式二:

<bean id="users1" class="......">
<bean id="users1" class="......">
<property name="FieldName">
	<map>
		<entry key="key1" value-ref="users1"/>
		<entry key="key2" value-ref="users2"/>
	</map>
</property>

注入Properties

<property name="FieldName" >
	<props>
		<prop key="KeyName">Content</prop>
	</props>
</property>

注解实现IOC

E:\java\Spring\spring_ioc2

Component

作用:用于创建对象,放入Spring容器,相当于 <bean id="" class="">
注意:

  1. 要在配置文件中配置扫描的包,扫描到该注解才能生效。
    context:component-scan base-package="com.itbaizhan"> </context:component-scan>
  2. @Component 注解配置bean的默认id是首字母小写的类名。也
    可以手动设置bean的id值。
/ 此时bean的id为studentDaoImpl
@Component
public class StudentDaoImpl implements
StudentDao{
    public Student findById(int id) {
        // 模拟根据id查询学生
        return new Student(1,"百战程序
员","北京");
   }
 }


// 此时bean的id为studentDao
@Component("studentDao")
public class StudentDaoImpl implements
StudentDao{
    public Student findById(int id) {
        // 模拟根据id查询学生
        return new Student(1,"百战程序
员","北京");
   }
}

@Repository、@Service、@Controller

作用:这三个注解和@Component的作用一样,使用它们是为了区分该类属于什么层。

  • @Repository用于Dao层
  • @Service用于Service层
  • @Controller用于Controller层

@Scope指定bean的创建策略:singleton prototype request session globalsession

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

pk5515

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

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

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

打赏作者

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

抵扣说明:

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

余额充值