Spring学习2
中文文档:https://blog.csdn.net/li1376417539/article/details/104951358
https://snailclimb.gitee.io/javaguide/#/
1. 依赖注入------Set注入
- 创建Address类
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Address() {
}
}
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties properties;
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", properties=" + properties +
'}';
}
}
- 创建Student类
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties properties;
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", properties=" + properties +
'}';
}
}
- 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.lyb.pojo.Address">
<property name="address" value="深圳"/>
</bean>
<bean id="student" class="com.lyb.pojo.Student">
<!--普通值注入-->
<property name="name" value="张三"/>
<!--bean注入-->
<property name="address" ref="address"/>
<!--数组注入-->
<property name="books">
<array>
<value>java</value>
<value>c++</value>
</array>
</property>
<!--list集合注入-->
<property name="hobbies">
<list>
<value>足球</value>
<value>篮球</value>
</list>
</property>
<!--map注入-->
<property name="card">
<map>
<entry key="身份证" value="12343423"/>
<entry key="校园卡" value="23234343"/>
</map>
</property>
<!--Set-->
<property name="games">
<set>
<value>lol</value>
<value>bob</value>
</set>
</property>
<!--Properties-->
<property name="properties">
<props>
<prop key="username">张三</prop>
<prop key="password">123</prop>
</props>
</property>
<!--null-->
<property name="wife">
<null/>
</property>
</bean>
</beans>
- 测试
@Test
public void Test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
- 结果
Student{name=‘张三’, address=com.lyb.pojo.Address@481a15ff, books=[java, c++], hobbies=[足球, 篮球], card={身份证=12343423, 校园卡=23234343}, games=[lol, bob], wife=‘null’, properties={password=123, username=张三}}
2. bean 的自动装配------XML
<!--使用byName自动装配
会在容器中寻找和对象set方法后的名字对应的beanId 如:setApple id="apple"-->
<bean id="people" class="lyb.pojo.People" autowire="byName">
<property name="name" value="李四"/>
</bean>
<!--使用byType自动装配
会在容器中寻找和对象属性类型相同的bean -->
<bean id="people" class="lyb.pojo.People" autowire="byName">
<property name="name" value="李四"/>
</bean>
3. bean 的自动装配------使用注解 @Autowired 进行装配
@Autowired注解优先使用根据类型进行标注装配
配置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
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:annotation-config/>
</beans>
注意:使用注解要引入命名空间,并且使用<context:annotation-config/>
开启注解。
除此之外:@Qualifier配合 @Autowired使用 可以指定唯一的bean对象注入!
@Autowired
@Qualifier(value = "apple1")
private Apple apple;
4. 使用注解开发
配置applicationContest.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
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:annotation-config/>
<!--配置注解扫描包-->
<context:component-scan base-package="com.lyb.pojo"/>
</beans>
注意:使用<context:annotation-config/>
开启注解,使用<context:component-scan base-package="com.lyb.pojo"/>
配置扫描包。
-
@Component :相当于
<bean id="user" class="com.lyb.pojo.User"></bean>
beanId=实体类名小写:user -
@Value:相当于
<property name="name" value="zhnagsan"/>
@Component
public class Eat {
@Value("米饭")
public String rice;
@Value("苹果")
public String apple;
}
- 配置类
@Configuration: 说明该类为配置类。
@ComponentScan: 扫描包。
@Bean: 通过方法注册一个bean,这里的返回值就Bean的类型,方法名就是bean的id!
@Configuration
public class SpringConfig {
@Bean
public User user(){
return new User();
}
}
@Component
public class User {
// public String name = "李四";
@Value("李四")
public String name;
}
public class SpringTest {
@Test
public void Test1(){
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
User user = (User) applicationContext.getBean("user");
System.out.println(user.name);
}
}
注意: 配置类使用AnnotationConfigApplicationContext(SpringConfig.class);
读取配置文件。
- @Import(): 当有多个配置类时,可以在配置类中使用
@Import()
导入其他配置类。