您的位置:首页 > 编程语言 > Java开发

Spring 的XML注入和注解注入混合使用

2019-02-15 15:25 316 查看

Spring 的XML注入和注解注入混合使

方式 优点
XML 结构清晰易于阅读
Annotation 开发便捷,注入方便
  1. 引入context命名空间
  2. 在配置文件中添加context:annotation-config标签
  3. XML管理类,注解进行属性注入

下面通过代码演示

文件目录

diDemo
- src
- - main
- - - java
- - - - com.zhangxin9727
- - - - - Persion.java
- - - - - Cat.java
- - - resources
- - - - applicationContext.xml
- - test
- - - java
- - - - MyTest.java
- pom.xml

详细代码

pom.xml

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

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

<groupId>com.zhangxin9727</groupId>
<artifactId>diDemo</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</project>

Person.java

package com.zhangxin9727;

import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;

public class Person {
@Value("Alice")
private String name;
@Resource(name="cat")
private Cat cat;

@Override
public String toString() {
return "Person{" + "name='" + name + '\'' + ", cat=" + cat + '}';
}
}

Cat.java

package com.zhangxin9727;

import org.springframework.beans.factory.annotation.Value;

public class Cat {
@Value("Kitty")
private String name;

@Override
public String toString() {
return "Cat{" + "name='" + name + '\'' + '}';
}
}

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

<context:annotation-config/>

<bean id="person" class="com.zhangxin9727.Person"/>
<bean id="cat" class="com.zhangxin9727.Cat"/>
</beans>

MyTest.java

import com.zhangxin9727.Person;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
@Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) context.getBean("person");
System.out.println(person);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐