美文网首页
spring注解01 bean xml配置文件注入

spring注解01 bean xml配置文件注入

作者: 運河的縴夫 | 来源:发表于2019-01-05 11:16 被阅读0次

1.项目结构

项目结构图.png

1.1 pom.xml

<dependencies>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
           <version>5.1.3.RELEASE</version>
       </dependency>
       <dependency>
           <groupId>org.projectlombok</groupId>
           <artifactId>lombok</artifactId>
           <version>1.18.4</version>
           <scope>provided</scope>
       </dependency>
   </dependencies>

1.2 Person.java

@Data
public class Person {

    private String name;
    private int age;
    private String nickName;

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

2,xml配置注入

2.1 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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id = "person" class="com.tommy.bean.Person">
        <property name="age" value="16"/>
        <property name="nickName" value="tommy"/>
        <property name="name" value="jm"/>
    </bean>
</beans>

2.2 测试类

public class MainTest {
    public static void main(String[] args) {
       final ClassPathXmlApplicationContext xmlApplicationContext = new ClassPathXmlApplicationContext("beans.xml");
       final Person person = (Person) xmlApplicationContext.getBean("person");
       System.out.println(person);
   }
}

2.3 测试结果

 com.tommy.MainTest
person = [Person(name=jm, age=16, nickName=tommy)]
person01

Process finished with exit code 0

相关文章

网友评论

      本文标题:spring注解01 bean xml配置文件注入

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