美文网首页
spring-3-配置文件中使用 bean

spring-3-配置文件中使用 bean

作者: blank_white | 来源:发表于2020-06-22 19:28 被阅读0次
<?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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

<!--    导入其他 spring 配置文件-->
<!--    <import resource="classpath:spring-*.xml"></import>-->
    <import resource="classpath:spring-school.xml"></import>
    <import resource="classpath:spring-student.xml"></import>

<!--    用注解方式的类所在包-->
    <context:component-scan base-package="com.spyouth.service;com.spyouth.learner"/>

    <bean id="someService" class="com.spyouth.service.SomeServiceImpl" />
<!--    单例模式-->
    <bean id="someService1" class="com.spyouth.service.SomeServiceImpl" scope="singleton"/>
<!--    原型模式-->
    <bean id="someService2" class="com.spyouth.service.SomeServiceImpl" scope="prototype"/>

<!--    声明切面类-->
    <bean id="aspectService" class="com.spyouth.service.AspectService"/>
<!--    默认为 false (推荐) ,表示使用 jdk 动态代理, true 表示使用 cglib 继承代理,如果目标类没有声明接口则自动使用 cglib -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>


    <bean id="student1" class="com.spyouth.learner.Student">
<!--        为属性赋值 ,java 基础类 数值、字符串 用 value-->
        <property name="name" value="张三"></property>
        <property name="age" value="18"></property>
<!--        引用类型 用 ref = 应用类型对象的名称 赋值-->
        <property name="school" ref="school"></property>
    </bean>
<!--    autowire="byName" : 自动通过名称,为引用类型属性赋值 -->
    <bean id="student2" class="com.spyouth.learner.Student" autowire="byName">
        <property name="name" value="李四"></property>
        <property name="age" value="19"></property>
    </bean>
    <!--    autowire="byType" : 自动通过查找匹配的类型实例,为引用类型的属性赋值 -->
    <bean id="student3" class="com.spyouth.learner.Student" autowire="byType">
        <property name="name" value="王五"></property>
        <property name="age" value="20"></property>
    </bean>
    
    <bean id="student4" class="com.spyouth.learner.Student" autowire="byType">
<!--        通过构造方法为属性赋值-->
        <constructor-arg name="name" value="21"></constructor-arg>
        <constructor-arg name="age" value="21"></constructor-arg>
        <constructor-arg name="school" ref="school"></constructor-arg>
    </bean>

    <bean id="student5" class="com.spyouth.learner.Student" autowire="byType">
<!--        通过构造方法为属性赋值,按参数列表的顺序-->
        <constructor-arg index="0" value="22"></constructor-arg>
        <constructor-arg index="1" value="22"></constructor-arg>
        <constructor-arg index="2" ref="school"></constructor-arg>
    </bean>

    <bean id="student6" class="com.spyouth.learner.Student" autowire="byType">
<!--        通过构造方法为属性赋值,按参数列表的顺序,按 index= 0,1,2,3···-->
        <constructor-arg value="23"></constructor-arg>
        <constructor-arg value="23"></constructor-arg>
        <constructor-arg ref="school"></constructor-arg>
    </bean>
</beans>

相关文章

网友评论

      本文标题:spring-3-配置文件中使用 bean

      本文链接:https://www.haomeiwen.com/subject/ucpowhtx.html