文章目录
Spring 使用注解代替xml
jar包
工程目录
配置文件
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:component-scan base-package="test"></context:component-scan>
</beans>
代码
package test;
public interface UserService {
public void addUser();
}
package test;
import org.springframework.stereotype.Component;
@Component("userServiceId")
public class UserServiceImpl implements UserService{
@Override
public void addUser() {
System.out.println("test.UserServiceImpl.addUser");
}
}
测试文件
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.UserService;
public class Execute {
@Test
public void demo02(){
//从spring容器获得
String xmlPath = "resource/applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
UserService userService = (UserService) applicationContext.getBean("userServiceId");
userService.addUser();
}
}
结果展示
Spring 依赖注入
普通值注入
配置文件
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:component-scan base-package="test"></context:component-scan>
</beans>
代码
package test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("user")
public class User {
@Value("xieyaoyan")
private String name;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
测试文件
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.StudentAction;
import test.User;
public class Execute {
@Test
public void demo02(){
//从spring容器获得
String xmlPath = "resource/applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
User studentAction = (User) applicationContext.getBean("user");
System.out.println("studentAction = " + studentAction);
}
}
结果展示
对象注入
配置文件
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:component-scan base-package="test"></context:component-scan>
</beans>
代码
package test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller("studentActionId")
public class StudentAction {
@Autowired //默认按照类型
private StudentService studentService;
public void execute() {
studentService.addStudent();
}
}
package test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao;
/**
* @Autowired
* @Qualifier("studentDaoId")
* 上面两行为一种方式
* 下面一行为另一种方式
* @Resource(name = "studentDaoId")
* @param studentDao
*/
@Autowired
@Qualifier("studentDaoId")
// @Resource(name = "studentDaoId")
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public void addStudent() {
studentDao.save();
}
}
package test;
public interface StudentDao {
void save();
}
package test;
import org.springframework.stereotype.Repository;
@Repository("studentDaoId")
public class StudentDaoImpl implements StudentDao {
@Override
public void save() {
System.out.println("dao");
}
}
package test;
public interface StudentService {
void addStudent();
}
测试代码
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.StudentAction;
public class Execute {
@Test
public void demo02(){
//从spring容器获得
String xmlPath = "resource/applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
StudentAction studentAction = (StudentAction) applicationContext.getBean("studentActionId");
studentAction.execute();
}
}
结果展示
初始化:@PostConstruct
销毁:@PreDestroy
@Scope(“prototype”) 多例
public interface UserService {
public void addUser();
}
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@Service("userServiceId")
//@Scope("prototype")
public class UserServiceImpl implements UserService {
@Override
public void addUser() {
System.out.println("d_scope add user");
}
@PostConstruct
public void myInit(){
System.out.println("初始化");
}
@PreDestroy
public void myDestroy(){
System.out.println("销毁");
}
}
注意
<context:annotation-config>
该标签,只在xml和注解(注入)混合使用时,使注入注解生效。