pyqb

导航

 

注解:代码里面特殊的标记,使用注解可以完成相关功能

注解写法:@注解名称(属性名、属性值)

 

@Required

用在set方法上,一旦用了这个注解,那么容器在初始化bean的时候必须要进行set,也就是说必须对这个值进行依赖注入。

 

编写Student.java

 1 package com.example.spring;
 2 import org.springframework.beans.factory.annotation.Required;
 3 
 4 public class Student {
 5     private String name;
 6 
 7     @Required
 8     public void setName(String name){
 9         this.name = name;
10     }
11 
12     public void print(){
13         System.out.println("name:"+name);
14     }
15 }

 

编写Beans.xml

 1 <?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">
 9 
10     <context:annotation-config/>
11 
12     <bean id="student" class="com.example.spring.Student" >
13         <property name="name" value="XiaoMing"></property>
14     </bean>
15 
16 </beans>

 

编写Application.java

 1 package com.example.spring;
 2 
 3 import org.springframework.context.support.AbstractApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class Application {
 7     public static void main(String[] args) {
 8         //bean配置文件所在位置 D:\\IdeaProjects\\spring\\src\\Beans.xml
 9         //使用AbstractApplicationContext容器
10         AbstractApplicationContext context = new ClassPathXmlApplicationContext("file:D:\\IdeaProjects\\spring\\src\\Beans.xml");
11         //得到配置创建的对象
12         Student student = (Student) context.getBean("student");
13         student.print();
14     }
15 }

 

创建对象的注解有四个注解

  1. @Component :对那些比较中立的类进行注释
  2. @Controller :WEB层
  3. @Service  :业务层
  4. @Repository  :持久层

 

@Component

在类定义上面使用,表示用注解方式创建对象,相当于在配置文件中,<bean id="" class="">

编写User.java

 1 package com.example.spring;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 @Component(value = "user") //相当于beans.xml中的<bean id="user" class="com.example.spring.User">
 6 public class User {
 7     public void print(){
 8         System.out.println("user pirnt.");
 9     }
10 }

编写Beans.xml

 1 <?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">
 9 
10     <!--开启注解扫描,到包里面扫描类、方法、属性上面是否有注解-->
11     <context:component-scan base-package="com.example.spring"></context:component-scan>
12 
13 </beans>

 编写Application.java

 1 package com.example.spring;
 2 
 3 import org.springframework.context.support.AbstractApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class Application {
 7     public static void main(String[] args) {
 8         //bean配置文件所在位置 D:\\IdeaProjects\\spring\\src\\Beans.xml
 9         //使用AbstractApplicationContext容器
10         AbstractApplicationContext context = new ClassPathXmlApplicationContext("file:D:\\IdeaProjects\\spring\\src\\Beans.xml");
11         //得到配置创建的对象
12         User user = (User) context.getBean("user");
13         user.print();
14     }
15 }

运行输出

user pirnt.

其他的注解创建对象方式类似 

 

 

 

注入对象属性的注解:

  1. @Autowired
  2. @Resource

 

@Autowired

对象类型属性注入注解

编写UserDao.java

 1 package com.example.spring;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 //@Component 创建对象注释,相当于在配置文件中中的<bean id="userDao" class="com.example.spring.UserDao">
 6 @Component(value = "userDao")
 7 public class UserDao {
 8     public void print(){
 9         System.out.println("Dao print.");
10     }
11 }

编写UserService.java

 1 package com.example.spring;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Component;
 5 
 6 //@Component 创建对象注释,相当于在配置文件中中的<bean id="userService" class="com.example.spring.UserService">
 7 @Component(value = "userService")
 8 public class UserService {
 9     //1、定义dao类型属性,在dao属性上面使用注解,完成对象注入
10     //@Autowired 相当于配置文件中的对象类型注入(如下)
11     //  <bean id="userDao" class="com.example.spring.UserDao"></bean>
12     //  <bean id="userService" class="com.example.spring.UserService" >
13     //      <property name="userDao" ref="userDao"></property>
14     //  </bean>
15     @Autowired
16     private UserDao userDao;
17     //2、使用注解方式时候不需要set方法
18     //public void setUserDao(UserDao userDao){
19     //    this.userDao = userDao;
20     //}
21 
22     public void print(){
23         System.out.println("Service print.");
24         userDao.print();
25     }
26 }

编写Beans.xml

 1 <?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">
 9 
10     <!--开启注解扫描,到包里面扫描类、方法、属性上面是否有注解-->
11     <context:component-scan base-package="com.example.spring"></context:component-scan>
12 
13 
14 </beans>

编写Application.java

 1 package com.example.spring;
 2 
 3 import org.springframework.context.support.AbstractApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class Application {
 7     public static void main(String[] args) {
 8         //bean配置文件所在位置 D:\\IdeaProjects\\spring\\src\\Beans.xml
 9         //使用AbstractApplicationContext容器
10         AbstractApplicationContext context = new ClassPathXmlApplicationContext("file:D:\\IdeaProjects\\spring\\src\\Beans.xml");
11         //得到配置创建的对象
12         UserService user = (UserService) context.getBean("userService");
13         user.print();
14     }
15 }

运行输出

Service print.
Dao print.

 

@Resource

@Autowired是通过类名来自动注入,和@Component(value = "userDao")中的value值无关,这时就引出了@Resource注解,@Resource(name="userDao"),就会查找@Component(value = "userDao")注解的类并注入。

posted on 2017-11-07 17:30  没有音乐就退化耳朵  阅读(253)  评论(0编辑  收藏  举报