Spring注解开发

JavaConfig

从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中。但是,仍然允许使用经典的XML方式来定义bean和配置,JavaConfig是另一种替代解决方案。所以,在Spring3以后的版本中,支持xml方式和javaConfig两种Spring配置方式。

建议:Spring项目用全注解开发,为后期学Spring Boot和Spring Cloud打好基础。

通过XML配置

Person.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.spring;

public class Person {
private String name;
private String age;

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

public Person() {
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}

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

bean.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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
class:bean的全类名,通过反射的方式在IOC容器中创建Bean,所以要求Bean中必须有无参数的构造器
id:标识容器中的bean。id唯一
-->
<bean id="person" class="com.Spring.Person">
<property name="name" value="HuiProgramer"></property>
<property name="age" value="21"></property>
</bean>
</beans>

Maintest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.Spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {
public static void main(String[] args){
//1.创建Spring 的IOC容器对象
//ApplicationContext 代表IOC容器
//ClassPathXmlApplicationContext:是ApplicationContext 接口的实现类
ApplicationContext act = new ClassPathXmlApplicationContext("bean.xml");
//2.从IOC容器中获取Bean实例
Person person = (Person)act.getBean("person");
//3.调用toString()方法并打印
System.out.println(person.toString());
}
}
}

结果:
Person{name='HuiProgramer', age='21'}

以上为Xml配置的方式,Spring3.0后启用注解开发模式。

使用注解模式

Person.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.spring;

public class Person {
private String name;
private String age;

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

public Person() {
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}

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

AppConfig.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//相当于beans
@Configuration
public class AppConfig {
public AppConfig(){
}
//相当于bean,默认方法名为Bean的id
@Bean
public Person person(){
return new Person("HuiPerson","21");
}
}

MainTest.Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Maintest{
public static void main(String[] args){
//通过注解获取ApplicationContext
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
//通过类获取Person的bean实例
Person person = context.getBean(Person.class);
//打印person
System.out.println(person);
//通过类型获取所有Bean的person bean实例
String[] names = context.getBeanNamesForType(Person.class);
//打印
for(String name:names)
System.out.println(name);
}
}

结果:
Person{name='HuiProgramer', age='21'}
person

此为Spring3.0后的注解开发模式!

-------------本文结束 感谢您的阅读-------------
文章对我有益,我要小额赞赏...