Spring——配置Spring

1. 用 XML 配置

1.1 包依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zth.Spring</groupId>
    <artifactId>spring02</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <spring.version>5.1.4.RELEASE</spring.version>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.4.RELEASE</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.26</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.26</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <finalName>spring02</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

1.2 java code

public class Teacher {
    private String name;
    private int age;

    /*getter setter toString*/
}

public class Student {
    private String name;
    private int age;
    private Teacher teacher;

    /*getter setter toString*/
}

1.3 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
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="teacher" class="com.zth.pojo.Teacher">
        <property name="name" value="李老师"/>
        <property name="age" value="34"/>
    </bean>

    <bean id="stu" class="com.zth.pojo.Student">
        <property name="name" value="zth"/>
        <property name="age" value="18"/>
        <property name="teacher" ref="teacher"/>
    </bean>

</beans>

1.4 测试

@Test
    public void testXML(){
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        Student student = (Student)context.getBean("stu");
        System.out.println(student);
    }

执行结果:

 

2. 用 Annotation 自动扫描装配

2.1 包依赖

同上

2.2 java code

@Component
public class Teacher {
    private String name;
    private int age;

    public Teacher(){
        this.name = "刘老师";
        this.age = 36;
    }

    public Teacher(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    /*getter setter toString*/
}

@Component
public class Student {
    private String name;
    private int age;

    @Autowired
    private Teacher teacher;

    public Student() {
        this.name = "zth";
        this.age = 18;
    }

    public Student(String name, int age, Teacher teacher) {
        this.name = name;
        this.age = age;
        this.teacher = teacher;
    }

    /*getter setter toString*/
    
}

2.3 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
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="com.bean"/>

</beans>

2.4 测试


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value={"/ApplicationContext.xml"})
public class TestAnno {

    @Autowired
    Student student;
    @Test
    public void testXML(){
        System.out.println(student);
    }
}

测试结果:

 

3. 用 Java 显式配置

3.1 包依赖

同上

3.2 java code

3.2.1 java bean

@Component
public class Teacher {
    private String name;
    private int age;

    public Teacher() {
        this.name = "王老师";
        this.age = 38;
    }

    public Teacher(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // toString
}

public class Student {
    private String name;
    private int age;
    @Autowired
    private Teacher teacher;
    
    /*getter setter toString*/
}

3.2.2 Configuration

@Configuration
@ComponentScan(basePackages = {"com"})
public class StuConfig {

    @Bean
    public Student getStudent(){
        Student student = new Student();
        student.setName("张三");
        student.setAge(18);
        return student;
    }
}

【注】必须放在根包下(包路径如下)

 

3.3 测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {StuConfig.class})
public class MyTest {
    @Autowired
    Student student;

    @Test
    public void test(){
        System.out.println(student);
    }
}

执行结果:

 

 4. 混合配置

4.1 在 JavaConfig 中引用 XML 配置

@Configuration
@import(AppConfig.class)
@importResource(“classpath:dao.xml”)
Public class AppConfig2{}

4.2  在 XML 中引用 JavaConfig

<beans>
    <import resource=”dao.xml”/>
    <bean id=”appconfig” class=”com.AppConfig”/>
</beans>

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值