注解的定义:注解是代码中特殊的标记,使用注解可以完成特定的功能。注解可以使用在类、方法或属性上,写法如:@注解名称(属性名称=属性值)
。
我的理解:注解就是给运行程序代码进行的注释,就像我们在学习古诗文时候的注释类似,不过spring框架中的注解不是给我们来看的,而是给机器来看的,让虚拟机知道我们需要它干什么,好处就是减少了代码量和重复代码以及减少了手动配置,以及增加了扩展性。
注解的第一个简单了解
环境:windows10、jdk1.8、
导入的包:commons-logging、log4j、spring-beans、spring-context、spring-core、spring-expression、spring-aop
工具:idea
代码举例:(读者可以和之前的不用注释时候的代码进行比较)
1、创建一个java类
package com.xawl.spring.annotate;
import org.springframework.stereotype.Component;
@Component(value = "user")
public class User {
public void add(){
System.out.println("this is annotate");
}
@Override
public String toString() {
return "User{}";
}
}
2、配置spring.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<!--注解的配置-->
<!--1、开启注解-->
<context:annotation-config></context:annotation-config>
<!--2、指定要扫描的包-->
<context:component-scan base-package="com.xawl.spring.annotate"></context:component-scan>
</beans>
3、创建测试类
package springannotate;
import com.xawl.spring.annotate.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserTest {
public static void main(String[] args) {
//获取数据源
String resources = "spring.xml";
//创建对象
ApplicationContext context = new ClassPathXmlApplicationContext(resources);
User user = (User) context.getBean("user");
System.out.println(user);
user.add();
}
}
4、查看打印结果
User{}
this is annotate
上面是一个最基本的使用注解的一个配置,我们使用注解创建了user对象,并成功的调用了add().
常用注解:
@Component :标准一个普通的spring Bean类。
@Repository:标注一个DAO组件类。
@Service:标注一个业务逻辑组件类。
@Controller:标注一个控制器组件类。
这几种注解的用法相同,作用也相同,区别在于标注组件的不同。
@Component可代替其它三个注解,因为其它三个注解是被@Component标注的。
使用注解方式注入属性:
在上一篇中介绍了属性的注入方式,现在来介绍使用注解注入属性。同样采用使用比较多的将doa层属性注入到service中
1、创建dao层java类
package com.xawl.spring.dao;
import org.springframework.stereotype.Repository;
@Repository(value = "annotateDao")
public class AnnotateDao {
public void add(){
System.out.println("this is annotatedao");
}
}
2、创建service
package com.xawl.spring.service;
import com.xawl.spring.dao.AnnotateDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service(value = "annotateService")
public class AnnotateService {
@Autowired
private AnnotateDao annotateDao;
public void add(){
System.out.println("this is service annotate");
annotateDao.add();
}
@Override
public String toString() {
return "AnnotateService{" +
"annotateDao=" + annotateDao +
'}';
}
}
3、配置spring,加入扫描包即可
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<!--注解的配置-->
<!--1、开启注解-->
<context:annotation-config></context:annotation-config>
<!--2、指定要扫描的包-->
<context:component-scan base-package="com.xawl.spring.annotate"></context:component-scan>
<context:component-scan base-package="com.xawl.spring.dao"></context:component-scan>
<context:component-scan base-package="com.xawl.spring.service"></context:component-scan>
4、创建测试类
package springannotate;
import com.xawl.spring.service.AnnotateService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AnnotateServiceTest {
public static void main(String[] args) {
///获取数据源
String resources = "spring.xml";
//创建对象
ApplicationContext context = new ClassPathXmlApplicationContext(resources);
AnnotateService service = (AnnotateService) context.getBean("annotateService");
System.out.println(service);
service.add();
}
}
5、输出结果
AnnotateService{annotateDao=com.xawl.spring.dao.AnnotateDao@1442d7b5}
this is service annotate
this is annotatedao
在这里使用了一个没有介绍的注解
@Autowired:自动装配
其它常用注解:
@Autowired:属于Spring 的org.springframework.beans.factory.annotation包下,可用于为类的属性、构造器、方法进行注值
@Resource:不属于spring的注解,而是来自于JSR-250位于java.annotation包下,使用该annotation为目标bean指定协作者Bean。
@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
还有许多的注解,我也正在学习中,读者可参见其它优秀的作品讲解。