Spring1.3——bean装配基于XML或注解

Javaweb框架学习文章索引点这里
基于xml的bean装配:
1,构造方法装配属性

<constructor-arg>:用于配置构造方法一个参数argument
name :参数的名称
value:设置普通数据
ref:引用数据,一般是另一个bean idindex :参数的索引号,从0开始 。如果只有索引,匹配到了多个构造方法时,默认使用第一个。
type :确定参数类型

例子:

    <!--其会自动调用Car类中第一个是int,第二个是String类型的构造函数并进行赋值-->
    <bean id="Car" class="com.e_a_const.Car">
    <bean id="Car" class="com.e_a_const.Car">
        <constructor-arg index="0" type="int" value="10"></constructor-arg>
        <constructor-arg index="1" type="String" value="张三"></constructor-arg>
    </bean>
    </bean>

2,使用setter方法装配属性

<!--name对应属性名,value对应赋值内容,ref对应引用-->
对于普通数据 
<property name="" value="值">
对于引用数据
<property name="" ref="另一个bean">

例子:

<!--用setter方法为Person装配属性和引用属性-->
    <bean id="Person" class="com.e_a_setter.Person">
        <property name="id" value="10"></property>
        <property name="name" value="张三"></property>
        <property name="address" ref="Address"></property>
    </bean>

    <bean id="Address" class="com.e_a_setter.Address">
        <property name="addressName" value="四川"></property>
    </bean>

3,使用P命名空间装配属性
注意要导入新的约束条件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="Person" class="com.e_a_p.Person"
        p:id="11"
        p:name="张三"
        p:address-ref="Address"
    >   
    </bean>
    <bean id="Address" class="com.e_a_p.Address"
        p:addressName="四川"
    >
    </bean>
</beans>

4,SpEl表达式的使用

    <property name="" value="#{表达式}">
    #{123}、#{'jack'} : 数字、字符串
    #{beanId}   :另一个bean引用
    #{beanId.propName}  :操作数据
    #{beanId.toString()}    :执行方法
    #{T(类).字段|方法}   :静态方法或字段
<!--本例的意思为判断Person中的name是否为空,若为空则赋值为空,不为空将其改为全英文大写然后赋值给name-->
    <bean id="Person" class="com.e_b_spel.Person">
        <property name="name" value="#{Person.name?.toUpperCase()}"></property>
    </bean>

5,为集合属性装配

<bean id="MyColl" class="com.e_c_coll.MyColl">
        <property name="array">
            <array>
                <value>张三1</value>
                <value>张三2</value>
                <value>张三3</value>
            </array>
        </property>
        <property name="list">
            <list>
                <value>张三1</value>
                <value>张三2</value>
                <value>张三3</value>
            </list>
        </property>
        <property name="set">
            <set>
                <value>张三1</value>
                <value>张三2</value>
                <value>张三3</value>
            </set>
        </property>
        <property name="map">
            <map>
                <entry key="1" value="张三1"></entry>
                <entry key="2" value="张三2"></entry>
            </map>
        </property>
        <property name="props">
            <props>
                <prop key="3">李四1</prop>
                <prop key="4">李四2</prop>
            </props>
        </property>
    </bean>

基于注解装配:

1. @Component取代<bean class="">
    @Component("id") 取代 <bean id="" class="">
2.web开发,提供3@Component注解衍生注解(功能一样)取代<bean class="">
    @Repository :dao层
    @Service:service层
    @Controller:web层
3.依赖注入  ,给私有字段设置,也可以给setter方法设置
    普通值:@Value("")
    引用值:
        方式1:按照【类型】注入
            @Autowired
        方式2:按照【名称】注入1
            @Autowired
            @Qualifier("名称")
        方式3:按照【名称】注入2
            @Resource("名称")
4.生命周期
    初始化:@PostConstruct
    销毁:@PreDestroy
5.作用域
    @Scope("prototype") 多例

实例:
配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           ">
    <!--配置spring去扫描路径-->
    <context:component-scan base-package="com.f_anno"></context:component-scan>
</beans>

web层

package com.f_anno;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller("BookAction")
public class BookAction {
    @Autowired
    public BookService bookService;//按照类型注入

    public String execute() {
        System.out.println("action");
        return "success";
    }
}

service层

package com.f_anno;

public interface BookService {
    public void addBooks();
}
package com.f_anno;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class BookServiceImpl implements BookService{
    @Autowired
    @Qualifier("BookDao2")
    public BookDao bookDao;

    @Override
    public void addBooks() {
        System.out.println("service");
        bookDao.info();
    }
}

dao层

package com.f_anno;

public interface BookDao {
    public void info();
}
package com.f_anno;

import org.springframework.stereotype.Repository;

@Repository("BookDao1")
public class BookDaoImpl implements BookDao{

    @Override
    public void info() {
        System.out.println("book info");
    }
}
package com.f_anno;

import org.springframework.stereotype.Repository;

@Repository("BookDao2")
public class BookDaoImpl2 implements BookDao{

    @Override
    public void info() {
        System.out.println("bookdao2");
    }

}

测试类:

    @Test
    public void test1() throws Exception {
        String xmlPath = "com/f_anno/beans.xml";
        ApplicationContext appct = new ClassPathXmlApplicationContext(xmlPath);
        BookAction bookAction = appct.getBean("BookAction", BookAction.class);
        bookAction.bookService.addBooks();
    }

结果:

service
bookdao2

可见,@Qualifier(“BookDao2”)的限制效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值