spring配置

pom.xml,spring核心依赖

        <!-- 只需导入spring-context依赖,其他依赖maven会自动关联导入 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>

applictionContext.xm

<?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
       https://www.springframework.org/schema/beans/spring-beans.xsd">

   <!-- 一个bean标签,就是spring管理的一个类 -->
   <!-- id: 类的唯一标识,其实就是创建该类后的对象名,后续通过该id从容器中获取对象 -->
   <!-- class: 类的全限定名 -->
   <bean id="myClass" class="com.qf.MyClass"/>


   <!-- UserService对象 -->
   <bean id="userService" class="com.qf.service.impl.UserServiceImpl">
      <!-- 在开闭标签之间对属性赋值 -->
      <!-- <property>给属性赋值,name写属性名,
         value 给基本类型属性值
         ref   给引用类型赋值,引用另外一个对象的id
      -->
      <property name="userDao" ref="userDao"/>
   </bean>

   <!-- UserDao对象 -->
   <bean id="userDao" class="com.qf.dao.impl.UserDaoImpl"/>

   <!-- TestModel -->
   <bean id="testModel" class="com.qf.di.TestModel">
      <!-- set方式赋值,使用property标签 -->
      <!-- name是属性名,基本类型使用value赋值 -->
      <!-- 引用类型赋值用ref -->
      <property name="id" value="1"/>
      <property name="money" value="9999.9"/>
      <property name="age" value="18"/> <!-- 包装类也使用value赋值 -->
      <property name="name" value="理想"/> <!-- String使用value赋值 -->
      <property name="birthday" value="2000/01/01"/> <!-- 日期使用value,其模板yyyy/MM/dd -->

      <property name="skill">
         <array> <!-- 数组赋值使用array标签 -->
            <value>Java</value>
            <value>H5</value>
            <value>Linux</value>
         </array>
      </property>

      <property name="className">
         <list><!-- list赋值使用 -->
            <value>JavaEE2217</value>
            <value>JavaEE2217</value>
            <value>JavaEE2215</value>
            <value>JavaEE2212</value>
         </list>
      </property>

      <property name="phones">
         <set> <!-- set集合赋值 -->
            <value>110</value>
            <value>110</value>
            <value>120</value>
            <value>119</value>
         </set>
      </property>

      <property name="np">
         <map> <!-- map属性赋值 -->
            <entry key="Henan" value="河南"/>
            <entry key="Beijing" value="北京"/>
            <entry key="Hangzhou" value="杭州"/>
         </map>
      </property>
   </bean>

   <bean id="testModel2" class="com.qf.di.TestModel2">
      <!-- constructor-arg:代表构造器的一个参数 -->
      <!-- index:参数的下标,type: 参数类型, name: 参数名 -->
      <!-- 以上三个是确定给哪个参数辅助,不用同时使用 -->
      <constructor-arg name="age" value="28"/>
      <constructor-arg name="name" value="亚杰"/>
   </bean>


</beans>

2.app

<?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
       https://www.springframework.org/schema/beans/spring-beans.xsd">
   <!--  演示自动注入 -->

   <!--
      autowire 自动装配,两种方案
         byType: 通过类型赋值
            解释: spring容器创建HouseServiceImpl,会解析到该对象需要HouseDao这些的属性
                 spring容器中刚好有一个houseDao对象,类型刚好也是HouseDao,所以可以自动注入
         byName: 通过名字注入
            解释: 当需要注入属性时,byType是按类型注入,但是容器中如果同类型有多个,那就会不知道注入哪个而报错
            此时就可以通过对象名来注入
         Name是指 bean的id和属性名一直
    -->
   <bean id="houseService" class="com.qf.service.impl.HouseServiceImpl" autowire="byName"/>


   <bean id="houseDao" class="com.qf.dao.impl.HouseDaoImpl"/>
   <bean id="houseDao2" class="com.qf.dao.impl.HouseDaoImpl2"/>

</beans>

测试

public class TestSpringFactory {


    @Test
    public void test() {
        // 配置文件路径
        String path = "applicationContext.xml";

        // 通过配置文件,创建容器
        ApplicationContext context = new ClassPathXmlApplicationContext(path);

        // 从容器中获得对象(通过标签的id)
        // MyClass myClass = (MyClass) context.getBean("myClass");
        // myClass.test();

        MyClass myClass = context.getBean("myClass", MyClass.class);

        myClass.test();
    }

    /**
     * 现在是使用单元测试,将来这些都会放入服务器中,这些步骤了解
     * 需要明白的是:
     *      我们所需要的bean要从spring容器中获取
     */
    @Test
    public void testIOC() {
        String path = "applicationContext.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(path);

        // 获得业务层对象
        UserService userService = context.getBean("userService", UserService.class);
        userService.findUserById();

    }

    /**
     * 通过set方法注入
     */
    @Test
    public void testDIBySet() {
        String path = "applicationContext.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(path);

        TestModel testModel = context.getBean("testModel", TestModel.class);
        System.out.println(testModel );

    }

    /**
     * 通过构造方法注入
     */
    @Test
    public void testDIByConstructor() {
        String path = "applicationContext.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(path);

        TestModel2 testModel2 = context.getBean("testModel2", TestModel2.class);
        System.out.println(testModel2 );

    }

    /**
     * 通过自动注入
     */
    @Test
    public void testDIByAutowire() {
        String path = "applicationContext2.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(path);

        HouseService houseService = context.getBean("houseService", HouseService.class);
        houseService.findHouseById();

    }

}

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd">


    <!--
        IOC,默认创建对象是单实例(scope="singleton"),即每次从容器中获得的是同一个对象
        设置成scope="prototype",那就多实例,即每次从容器获得都是新对象
    -->
    <bean id="myClass" class="com.qf.model.MyClass" scope="prototype">
        <property name="age" value="18"/>
        <property name="name" value="京静"/>
    </bean>

    <bean id="userService" class="com.qf.service.impl.UserServiceImpl" autowire="byType"/>

    <bean id="userDao" class="com.qf.dao.impl.UserDaoImpl"/>
</beans>

applicationContextAnno.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://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.qf"/>

</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

想要入门的程序猿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值