Spring常用依赖与配置:XML配置版和注解版

1.XML配置版

在这里插入图片描述

1.pom.xml

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.10.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    
    <!--如果Spring要实现AOP,使用AOP织入,需要导入一个依赖包-->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.4</version>
    </dependency>
    
 </dependencies>

2.beans.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">

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions go here -->

</beans>

举例版:

<?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"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
    具p-命名空间的XML快捷方式加上:xmlns:p="http://www.springframework.org/schema/p"
    具c-namespace的XML快捷方式加上:xmlns:c="http://www.springframework.org/schema/c",C命名一定要有有参构造器
    -->
  
  <!--使用Spring来创建对象,在Spring这些都称为Bean

        类型 变量名  = new 类型();
        Address address= new Address();

        id = 变量名
        class = new 的对象;
        property 相当于给对象中的属性设置一个值!
        ref : 引用Spring容器中创建好的对象
        value : 具体的值,基本数据类型!
        -->
    <bean id="address" class="com.loey.pojo.Address" scope="prototype">
        <property name="address" value="hanguo"></property>
    </bean>

<!--    p命名-->
    <bean id="stu" class="com.loey.pojo.Student" p:name="pcy" p:address-ref="address" p:books="1,2,3"/>

<!--    c命名-->
    <bean id="stu1" class="com.loey.pojo.Student" c:name="pcyc" c:address-ref="address"/>

    <bean id="student" class="com.loey.pojo.Student">

        <property name="name" value="pcy"></property>

        <property name="address" ref="address"></property>

        <property name="books">
            <array>
                <value>1</value>
                <value>2</value>
                <value>3</value>
            </array>
        </property>

        <property name="hobbys">
            <list>
                <value>a</value>
                <value>b</value>
                <value>c</value>
                <value>d</value>
            </list>
        </property>

        <property name="card">
            <map>
                <entry key="1" value="11"/>
                <entry key="2" value="22"/>
                <entry key="3" value="33"/>
            </map>
        </property>

        <property name="games">
            <set>
                <value>aa</value>
                <value>bb</value>
            </set>
        </property>

        <property name="wife">
            <null/>
        </property>

        <property name="info">
            <props>
                <prop key="url">com.mysql.jdbc.Driver</prop>
                <prop key="username">url</prop>
                <prop key="password">1127</prop>
            </props>
        </property>
    </bean>


</beans>

3.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">

    <import resource="beans.xml"/>

</beans>

4.测试获取上下文对象

 //获取spring的上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        Student student = (Student) context.getBean("student");
        Student stu = context.getBean("stu", Student.class);
//        Student stu1 = context.getBean("stu1", Student.class);

//        System.out.println(student.toString());

        System.out.println(stu.toString());
//        System.out.println(stu1.toString());

2.注解版

1.pom.xml

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.10.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <!--如果Spring要实现AOP,使用AOP织入,需要导入一个依赖包-->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.4</version>
    </dependency>
 </dependencies>

2.beans.xml

利用注解的方式注入属性加上约束
xmlns:context=“http://www.springframework.org/schema/context”
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd

<?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">

    <!--指定要扫描的包,这个包下的注解就会生效-->
    <context:component-scan base-package="com.loey.pojo"/>
    <!--开启注解的支持-->
    <context:annotation-config/>



</beans>

3.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">

    <import resource="beans.xml"/>

</beans

详细看Spring使用注解开发

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值