202104042.png
淡煙疏雨清明日渡紫,飛絮落花游子心。
燕話春愁初睡起,一簾草色暮池深
Spring針對Bean管理中創(chuàng)建對象提供注解
(1) @Component
(2) @Service
(3) @Controller
(4)@Repository
上面的4個注解功能是一樣的灶挟,都可以用來創(chuàng)建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(); } }
-
組件掃描細節(jié)配置
<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>
-
-
基于注解方式實現(xiàn)屬性注解
@AutoWired 根據(jù)屬性類型自動裝配
- 把service和dao對象創(chuàng)建,在service和dao類添加創(chuàng)建對象注解
- 在service注入dao對象域帐,在service類添加dao類型屬性,在屬性上面使用注解
@Qualifier 根據(jù)屬性名稱注入
這個@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 可以根據(jù)類型龙优,可以根據(jù)名稱
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;
完全注解來發(fā)
- 創(chuàng)建配置類,替代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();
}