淡烟疏雨清明日,飞絮落花游子心。
燕话春愁初睡起,一帘草色暮池深
Spring针对Bean管理中创建对象提供注解
(1) @Component
(2) @Service
(3) @Controller
(4)@Repository
上面的4个注解功能是一样的,都可以用来创建bean的实例
-
第一步引入依赖
spring-aop.jar
-
开启组件扫描
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 开启组件扫描 --> <context:component-scan base-package="com.company.user"></context:component-scan> </beans>
测试:
package com.company.user; import org.springframework.stereotype.Component; @Component(value = "user") public class User { private String name; private int age; public User() { } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } public void show() { System.out.println("add....."); } }
package com.company.test; import com.company.user.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { @Test public void test() { ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); User user = context.getBean("user", User.class); System.out.println(user); user.show(); } }
-
组件扫描细节配置
<context:component-scan base-package="com.company.user" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/> </context:component-scan>
-
-
基于注解方式实现属性注解
@AutoWired 根据属性类型自动装配
- 把service和dao对象创建,在service和dao类添加创建对象注解
- 在service注入dao对象,在service类添加dao类型属性,在属性上面使用注解
@Qualifier 根据属性名称注入
这个@Qualifier注解需要跟!@AutoWired一起使用
package com.company.dao;
import org.springframework.stereotype.Repository;
@Repository(value = "userDaoImpl")
public class UserDaoImpl implements UserDao{
@Override
public void add() {
System.out.println("dao add。。。");
}
}
package com.company.service;
import com.company.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
@Qualifier(value = "userDaoImpl")
private UserDao userDao;
public void add() {
System.out.println("service add....");
userDao.add();
}
}
@Resource 可以根据类型,可以根据名称
package com.company.service;
import com.company.dao.UserDao;
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 UserService {
// @Autowired
// @Qualifier(value = "userDaoImpl")
// private UserDao userDao;
@Resource(name = "userDaoImpl")
private UserDao userDao;
public void add() {
System.out.println("service add....");
userDao.add();
}
}
@Value
@Value(value = "abc")
private String name;
完全注解来发
- 创建配置类,替代xml配置文件
package com.company.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.company")
public class SpringConfig {
}
@Test
public void test3() {
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}