In Part I use the @Autowired
to simplify development, but in fact can also simplify:
In Part I, the xml
designated class configuration, as follows:
<bean id="address" class="com.weizu.pojo.UserAddress" p:address="西安"/>
<bean id="weizu" class="com.weizu.pojo.User" p:name="张三">
<property name="age" value="21"></property>
</bean>
1. @Component
At this time, no need, we directly use component
to specify the address of annotation scanning, this time is xml
as follows:
<?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
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置注解扫描。 指定要扫描的包,这个包下的注解就会生效 -->
<context:component-scan base-package="com.weizu.pojo"/>
<context:annotation-config/>
</beans>
Then, specify in User.java
andUserAddress.java
@Component
package com.weizu.pojo;
import org.springframework.stereotype.Component;
/**
* @Component等价于
* <bean id="address" class="com.weizu.pojo.UserAddress" p:address="西安"/>
*/
@Component
public class UserAddress {
public String address = "西安";
@Override
public String toString() {
return "UserAddress{" +
"address='" + address + '\'' +
'}';
}
}
package com.weizu.pojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class User {
public int age = 12;
public String name = "张三";
@Autowired
private UserAddress address;
@Override
public String toString() {
return "User{" +
"age=" + age +
", name='" + name + '\'' +
", address=" + address +
'}';
}
}
Then test:
public class myTest {
@Test
public void Test(){
// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
// 此时在beans.xml中没有指定id,由于@Component是其等价写法,默认的id也就是类的名字的小写
User userService = (User) context.getBean("user");
System.out.println(userService.toString());
}
}
Since it beans.xml
is not specified id
in and @Component
is equivalent, the default id
is the lowercase of the name of the class, so it is written here user
.
Use @Component
placed on the class, indicating that this class is Spring
managed, that is bean
.
2. @Value
Use @Value
can specify the value of the attribute, can be placed in a defined position, can also be placed on the set
method. For the User
class, as follows:
@Component
public class User {
@Value("14")
private int age;
@Value("张三")
private String name;
@Autowired
private UserAddress address;
@Override
public String toString() {
return "User{" +
"age=" + age +
", name='" + name + '\'' +
", address=" + address +
'}';
}
}
The effect remains the same:
among them @Value("14")
, it is equivalent to<property name="age" value="14"></property>
3. Derivative annotations
Reference video: https://www.bilibili.com/video/BV1WE411d7Dv?p=14