spring框架IOC配置

第一步:导入包
commons-logging-1.2.jar
spring-beans-3.2.13.RELEASE.jar
spring-context-3.2.13.RELEASE.jar
spring-core-3.2.13.RELEASE.jar
spring-expression-3.2.13.RELEASE.jar
spring开头的 4个包自带的……
commons-log4j需要https://repo.spring.io 下载

第二步:配置ApplicationContext.xml文件

<?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"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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-3.2.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd">

   

       <!-- bean元素声明spring创建的对象实例,id属性指定对象唯一标识符,可以设置多个别名,用name属性替换id属性指定,用逗号隔开。 class属性指定被声明的实例对应类 -->
       <bean id="helloSpring" class="com.big3.dao.impl.HelloSpring" >
       		<!-- 指定对象被赋值的属性名赋值-->
       		<property name="who" value="Spring" />     			      
       </bean>
       
       <bean id="greyInk" class="com.big3.dao.impl.GreyInk" ></bean>

       <bean id="colorink" class="com.big3.dao.impl.ColorInk" ></bean>

       <bean id="a4paper" class="com.big3.dao.impl.TextPaper" >
       		<property name="charPerLine" value="8" />
       		<property name="linePerPage" value="6" />
       </bean>
       <!-- value给常量和变量赋值 -->
       <bean id="b5paper" class="com.big3.dao.impl.TextPaper" >
       		<property name="charPerLine" value="6" />
       		<property name="linePerPage" value="5" />
       </bean>
       <!-- ref赋对象值 -->
       <bean id="printer" class="com.big3.dao.impl.Printer" >
       		<property name="ink" ref="colorink" />
       		<property name="paper" ref="a4paper" />
       </bean>
       
    <bean id="usLogger" class="com.big3.aop.UserServiceLogger"/>

    <!--通过构造方法注入参数,也可以用C:标签-->
    <bean id="use" class="com.big3.pojo.User" c:_0="ssss" c:_1="1000">
        <!--<constructor-arg index="0" >
            <value>zsc</value>
        </constructor-arg>
        <constructor-arg index="1" >
            <value>18</value>
        </constructor-arg>-->
    </bean>
    <bean id="userServiceImpl" class="com.big3.service.impl.UserServiceImpl">
        <!-- 使用CDATA处理特殊字符 -->
        <property name="specialCharacter1">
            <value><![CDATA[P&G]]></value>
        </property>
        <!-- 将特殊字符转为实体引用 -->
        <property name="specialCharacter2">
            <value>P&amp;G</value>
        </property>
        <!-- JAVABEN赋值 -->
        <property name="innerBean">
            <bean class="com.big3.pojo.User">
                <property name="name"><value>zsc</value></property>
                <property name="age"><value>18</value></property>
            </bean>
        </property>
        <!-- List赋值,里面还可以加<bean>和<ref>标签 -->
        <property name="list">
            <list>
                <value>000</value>
                <value>111</value>

            </list>
        </property>
        <!-- 数组赋值,里面也可以用list换掉array标签 -->
        <property name="array">
            <array>
                <value>aaaa</value>
                <value>bbbb</value>
            </array>
        </property>
        <!-- set赋值 -->
        <property name="set">
            <set>
                <value>9999</value>
                <value>8888</value>
            </set>
        </property>
        <!-- Map赋值 -->
        <property name="map">
            <map>
                <entry>
                    <key><value>user</value></key>
                    <!--<value>100</value>-->
                    <bean  class="com.big3.pojo.User" p:name="123456" p:age="100"/>

                </entry>

            </map>
        </property>
        <!-- Properties赋值 -->
        <property name="props">
            <props>
                <prop key="name">wwww</prop>
                <prop key="age">50</prop>
            </props>
        </property>
        <!-- 空字符串赋值 -->
        <property name="emptyValue">
            <value></value>
        </property>
        <!-- null赋值 -->
        <property name="nullValue">
            <null/>
        </property>
    </bean>
</beans>

可以通过注解方法,就不用在xml文件里配置bean了,只需加入如下代码就可

<!--指定扫描注解定义的包-->
<context:component-scan base-package="cn.kgc.service,cn.kgc.pojo"></context:component-scan>

只需在类里加上注解即可:
@PropertySource(“zscbbs.properties”) //在类前面加入,引入配置文件
@Value("${user}") //给属性赋配置文件里的值

@ImportResource 的作用是导入Spring的配置文件,让配置文件里面的内容生效。

@Configuration 配置类,类似于配置文件
@Bean

@Component 实现Bean组件的定义
@Repository 标注DAO类
@Service 标注业务类
@Controller 标注控制器类,也就是servlet类

@Resource 对类的成员变量和方法进行标注,默认按 byName自动注入,JSR-250规范定义的注解,单独使用

@Autowired 对类的成员变量和方法进行标注,按byType自动注入,属于spring框架,和@Qualifier一起使用
@Qualifier(“userDao”)

1、 @Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上。
2、 @Autowired默认按类型装配(这个注解是属业spring的),默认情况下必须要求依赖对象必须存在,如果要允许null值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用
3、@Resource(这个注解属于J2EE的),默认按照名称进行装配,名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认取字段名进行安装名称查找,如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。
4.一个类的成员变量是List<>或者Map<>时,如果类型用接口类型,用@Autowired或者@Resource注解,自动会把此接口的实现类对象保存在集合里。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值