完整的pom.xml的
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>5.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-expressionartifactId>
<version>5.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>5.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>5.2.5.RELEASEversion>
dependency>
dependencies>
<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">
beans>
<context:component-scan base-package="com.springioc.demo1"/>
在demo1包下创建一个Person.java:
package com.springioc.demo1;
import org.springframework.stereotype.Component;
//括号里的值相当于 中的id属性值
@Component("person")
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
测试代码:
@Test
public void testPerson() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) applicationContext.getBean("person");
person.setName("张三");
person.setAge(20);
System.out.println(person);
}
这三个注解是为了让标注类本身的用途清晰,Spring在后续版本会对其增强
例如
...
@Value("饭")
private String food;
public void eat() {
System.out.println("吃" + food);
...
如果有set方法,也可以吧注释加在set上
private String food;
@Value("饭")
public void setFood(String food) {
this.food = food;
}
public void eat() {
System.out.println("吃" + food);
}
例如需要在UserService类中注入UserDao类
@Service("userService")
public class UserService {
@Autowired
private UserDao userDao;
public void save() {
System.out.println("Service中保存用户...");
userDao.save();
}
}
@Repository("userDao")
public class UserDao {
public void save() {
System.out.println("Dao中保存用户...");
}
}
@Service("userService")
public class UserService {
@Autowired(required = false)
@Qualifier("userDao")
private UserDao userDao;
public void save() {
System.out.println("Service中保存用户...");
userDao.save();
}
}
@Repository("userDao")
public class UserDao {
public void save() {
System.out.println("Dao中保存用户...");
}
}
@Service("userService")
public class UserService {
@Resource(name = "userDao")
private UserDao userDao;
public void save() {
System.out.println("Service中保存用户...");
userDao.save();
}
}
@Repository("userDao")
public class UserDao {
public void save() {
System.out.println("Dao中保存用户...");
}
}
Spring初始化bean或销毁bean时,有时需要作一些处理工作,因此spring可以在创建和拆卸bean的时候调用bean的两个生命周期方法。
配置bean的xml是这样写的
<bean id="person" class="com.springioc.demo3.Person" init-method="init" destroy-method="destroy"/>
init-method 属性指定初始化方法,相当于用注释@PostConstruct
destroy-method 属性指定摧毁方法,相当于用注释@PreDestroy
用法
@Component("bean")
public class Bean {
@PostConstruct
public void init() {
System.out.println("init-method");
}
public void say() {
System.out.println("hello");
}
@PreDestroy
public void destory() {
System.out.println("destroy-method");
}
}
用法:
@Component("demo")
@Scope("prototype")
public class Demo {
}
一般类交给xml管理,用注解属性注入