注解:代码里面特殊的标记,使用注解可以完成相关功能
注解写法:@注解名称(属性名、属性值)
@Required
用在set方法上,一旦用了这个注解,那么容器在初始化bean的时候必须要进行set,也就是说必须对这个值进行依赖注入。
编写Student.java
package com.example.spring;
import org.springframework.beans.factory.annotation.Required; public class Student {
private String name; @Required
public void setName(String name){
this.name = name;
} public void print(){
System.out.println("name:"+name);
}
}
编写Beans.xml
<?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans
6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
7 http://www.springframework.org/schema/context
8 http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <bean id="student" class="com.example.spring.Student" >
<property name="name" value="XiaoMing"></property>
</bean> </beans>
编写Application.java
package com.example.spring; import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application {
public static void main(String[] args) {
//bean配置文件所在位置 D:\\IdeaProjects\\spring\\src\\Beans.xml
//使用AbstractApplicationContext容器
AbstractApplicationContext context = new ClassPathXmlApplicationContext("file:D:\\IdeaProjects\\spring\\src\\Beans.xml");
//得到配置创建的对象
Student student = (Student) context.getBean("student");
student.print();
}
}
创建对象的注解有四个注解
- @Component :对那些比较中立的类进行注释
- @Controller :WEB层
- @Service :业务层
- @Repository :持久层
@Component
在类定义上面使用,表示用注解方式创建对象,相当于在配置文件中,<bean id="" class="">
编写User.java
package com.example.spring; import org.springframework.stereotype.Component; @Component(value = "user") //相当于beans.xml中的<bean id="user" class="com.example.spring.User">
public class User {
public void print(){
System.out.println("user pirnt.");
}
}
编写Beans.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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!--开启注解扫描,到包里面扫描类、方法、属性上面是否有注解-->
<context:component-scan base-package="com.example.spring"></context:component-scan> 13 </beans>
编写Application.java
package com.example.spring; import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application {
public static void main(String[] args) {
//bean配置文件所在位置 D:\\IdeaProjects\\spring\\src\\Beans.xml
//使用AbstractApplicationContext容器
AbstractApplicationContext context = new ClassPathXmlApplicationContext("file:D:\\IdeaProjects\\spring\\src\\Beans.xml");
//得到配置创建的对象
User user = (User) context.getBean("user");
user.print();
}
}
运行输出
user pirnt.
其他的注解创建对象方式类似
注入对象属性的注解:
- @Autowired
- @Resource
@Autowired
对象类型属性注入注解
编写UserDao.java
package com.example.spring; import org.springframework.stereotype.Component; //@Component 创建对象注释,相当于在配置文件中中的<bean id="userDao" class="com.example.spring.UserDao">
@Component(value = "userDao")
public class UserDao {
public void print(){
System.out.println("Dao print.");
}
}
编写UserService.java
package com.example.spring; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; //@Component 创建对象注释,相当于在配置文件中中的<bean id="userService" class="com.example.spring.UserService">
@Component(value = "userService")
public class UserService {
//1、定义dao类型属性,在dao属性上面使用注解,完成对象注入
//@Autowired 相当于配置文件中的对象类型注入(如下)
// <bean id="userDao" class="com.example.spring.UserDao"></bean>
// <bean id="userService" class="com.example.spring.UserService" >
// <property name="userDao" ref="userDao"></property>
// </bean>
@Autowired
private UserDao userDao;
//2、使用注解方式时候不需要set方法
//public void setUserDao(UserDao userDao){
// this.userDao = userDao;
//} public void print(){
System.out.println("Service print.");
userDao.print();
}
}
编写Beans.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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!--开启注解扫描,到包里面扫描类、方法、属性上面是否有注解-->
<context:component-scan base-package="com.example.spring"></context:component-scan> </beans>
编写Application.java
package com.example.spring; import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application {
public static void main(String[] args) {
//bean配置文件所在位置 D:\\IdeaProjects\\spring\\src\\Beans.xml
//使用AbstractApplicationContext容器
AbstractApplicationContext context = new ClassPathXmlApplicationContext("file:D:\\IdeaProjects\\spring\\src\\Beans.xml");
//得到配置创建的对象
UserService user = (UserService) context.getBean("userService");
user.print();
}
}
运行输出
Service print.
Dao print.
@Resource
@Autowired是通过类名来自动注入,和@Component(value = "userDao")中的value值无关,这时就引出了@Resource注解,@Resource(name="userDao"),就会查找@Component(value = "userDao")注解的类并注入。