這里的注解是最初級的一些注解,掌握了之后再學習其它的注解
注解掃描
<?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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--此標簽表示開啟注解掃描,即掃描對應包及其子包的注解-->
<context:component-scan base-package="com.ty"></context:component-scan>
</beans>
除了上述方式還可以指定過濾規則來進行注解掃描
<context:component-scan base-package="com.ty">
<!--排除過濾,排除包中的某些類
type="":指定過濾規則
annotation:按照注解進行排除,標注了指定注解的組件不要,expression表示要過濾的注解
assignable:指定排除某個具體的類,按照類排除,expression表示不注冊的具體類名
aspectj:后面講aop的時候說明要使用的aspectj運算式,不用
custom:定義一個typeFilter,自己寫代碼決定哪些類被過濾掉,不用
regex:使用正則運算式過濾,不用
-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--包含過濾,指定包中的類
需要先配置use-default-filters屬性,讓默認掃描方式失效,默認是掃描全部
剩下的過濾屬性跟排除過濾相同
-->
<context:component-scan base-package="com.ty" use-default-filters="false">
<context:include-filter type="aspectj" expression="com.ty.other.*"/>
</context:component-scan>
創建物件
@Component
此注解就是原配置方式中的<bean>標簽,即:<bean id="user" >
注解中組件的id默認是類名首字母小寫,class就是底層通過反射獲取User類資訊;通過@Component分別又衍生出三個注解
| 注解 | 含義、說明 |
|---|---|
| @Component | 沒有明顯含義 |
| @Controller | 控制器,推薦給controller層添加此注解 |
| @Service | 業務邏輯,推薦給業務邏輯層添加此注解 |
| @Repository | 資料庫管理,推薦給資料訪問層添加此注解 |
這4個注解產生的效果方式是相同的,只是為了方便閱讀,提高可讀性
@Component
public class User {
private String username;
private String password;
/*省略構造,getter/setter和toString方法 */
}
@Scope
設定bean物件的作用域,就是bean標簽中的scope屬性
@Lazy
延遲創建單實體物件,就是bean標簽中的lazy="false"屬性
@PostConstruct
@PreDestroy
這兩個注解表示bean物件的物件的生命周期,就是bean標簽中的init-method和destroy-method屬性
@PostConstruct
public void init() {
System.out.println("init");
}
@PreDestroy
public void destroy() {
System.out.println("destroy");
}
依賴注入
@Autowired
@Repository
public class UserDaoImpl implements UserDao {
@Override
public void findUser() {
System.out.println("查詢用戶!");
}
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public void login() {
userDao.findUser();
}
}
注意:此注解默認是按照型別進行自動裝配(就是注入)
1、如果只找到一個,則直接進行賦值,
2、如果沒有找到,則直接拋出例外,
3、如果找到多個,那么會按照變數名作為id繼續匹配,
-
匹配上直接進行裝配
-
如果匹配不上則直接報例外
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao2;
@Override
public void login() {
userDao2.findUser();
}
}
//UserServiceImpl2繼承UserServiceImpl類
@Service
public class UserServiceImpl2 extends UserServiceImpl {
@Autowired
private UserDao userDao;
@Override
public void login() {
System.out.println("UserServiceImpl2");
userDao.findUser();
}
}
@Controller
public class UserController {
@Autowired
private UserService userServiceImpl;
public void test() {
userServiceImpl.login();
}
}
方法上也可以有@Autowired注解
@Autowired
public void setUserService(UserService userServiceImpl) {
this.userService = userServiceImpl;
}
@Qualifier
此注解配套@Autowired使用,用來指定id的名字,讓其按照名字裝配;按照名字找到了就進行裝配,否則就報錯
@Controller
public class UserController {
@Autowired
@Qualifier("userServiceImpl")
private UserService userService;
public void test() {
userService.login();
}
}
@Autowired注解可以用在方法上,@Qualifier只可以用在引數串列中
@Autowired
public void test(@Qualifier("userServiceImpl2") UserService userService) {
System.out.println("此方法被呼叫:" + userService);
}
ps:方法上有@AutoWired注解時:bean創建時會自動呼叫,這個方法的每個引數都會自動注入
? @Qualifier注解作用在引數上時,指定屬性的id名字
@Resource
@Resource注解的作用和@Autowired的作用相同,而且@Resource(name="") = @Autowired+@Resource
public class UserController {
@Resource(name = "userServiceImpl")
private UserService userService;
}
等同于:
public class UserController {
@Autowired
@Qualifier("userServiceImpl")
private UserService userService;
}
兩者的功能相同,但還是有區別的:
@Autowired注解是Spring提供的,而@Resource是JavaEE規范提供的
?@Autowired注解默認按照型別裝配,而@Resource注解默認是按照名字裝配
@Value
屬性賦值,這種方式不能用在靜態變數上,而且不能注入集合型別
#先定義一個properties屬性檔案,并設定要注入屬性的值
username=jack
password=root
<!--把剛剛定義好的properties屬性檔案加載到組態檔中-->
<context:property-placeholder location="classpath:bean.properties"></context:property-placeholder>
或者在類上添加@PropertySource("bean.properties")注解
//在屬性上加 @Value("${key}") 注解
@Value("${username}")
private String username;
@Value("${password}")
private String password;
最后再看一下泛型依賴
泛型依賴
public class Student {
}
public class Teacher {
}
public interface BaseDao<T> {
void save();
}
//StudentDao.java
@Repository
public class StudentDao implements BaseDao<Student> {
@Override
public void save() {
System.out.println("保存學生!");
}
}
//TeacherDao.java
@Repository
public class TeacherDao implements BaseDao<Teacher> {
@Override
public void save() {
System.out.println("保存老師!");
}
}
public class BaseService<T> {
@Autowired
private BaseDao<T> baseDao;
public void save(){
baseDao.save();
}
}
//StudentService.java
@Service
public class StudentService extends BaseService<Student> {
}
//TeacherService.java
@Service
public class TeacherService extends BaseService<Teacher> {
}
//測驗
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
StudentService studentService = context.getBean("studentService", StudentService.class);
TeacherService teacherService = context.getBean("teacherService", TeacherService.class);
studentService.save();
teacherService.save();

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/240759.html
標籤:Java
上一篇:SpringBoot-初體驗
