Spring学习笔记②

Spring Bean管理器 (注解方式)

0、注解

  1. 注解是代码里面的标记,完成一些功能
  2. @ 注解名 = 注解值
  3. 注解几乎可以在代码的任何地方 类上 方法上 属性上

好处:可以替代一部分配置文件,减少了配置,但不会替代配置文件

1、注解案例

     1.引入aop的jar包 

     2.创建类

package tech.youngs.Annotation;

import org.springframework.stereotype.Component;public class User {
    public void add()
    {
        System.out.println("add............");
    }
}

     3.创建配置文件,引入新的约束

 

<?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 http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->

</beans>

    4.引入注解

package tech.youngs.Annotation;

import org.springframework.stereotype.Component;
//下面的注解相当于在配置文件里写<bean id="user" class="tech.youngs.Annotation.User"></bean>
//value对应配置文件的id
@Component(value="user") 
public class User {
    public void add()
    {
        System.out.println("add............");
    }
}

//下面的注解相当于在配置文件里写<bean id="user" class="tech.youngs.Annotation.User"></bean> //value对应配置文件的id @Component(value="user")

   5.测试类与使用配置文件方式创建bean一样

@Test
public void test()
{
     //引入Spring配置文件
     ApplicationContext ctx = new ClassPathXmlApplicaitonContext("applicaitonContext.xml");
     //使用bean管理器创建出对象
    User user = (User)ctx.getBean("user");
    user.add();
}

2、注解拓展

   除了@Component之外,Spring还为JavaEE的三层结构的每一层都提供了与@Component功能一致的注解,目的是层次清晰,容易区分。

       WEB                @Controller

       SERVICE          @Service

       DAO                @Repository    [Repository  n.仓库、储藏室]

 后续版本将对这三个注解做扩展。@Service @Controller最少记住 @Respository 最好记住

 创建对象是单实例还是多实例?

    @Scope(value = singleton | prototype)

3、注解方式注入属性

第一种方式:

//创建dao对象,并使用注解进行bean管理

@Repository("userDao")
public class UserDao {
    public void add()
    {
        System.out.println("dao..............");
    }
}

//创建service对象,并使用注解进行bean管理
@Service("userService")
public class UserService {    
    public void add()
    {
        System.out.println("service.........");
    }
}

//在需要注入的类中声明被注入类的属性,使用@Autowired注解进行注入即可
@Service("userService")
public class UserService {
    //在注入对象的属性上使用注解实现注入
    @Autowired
    private UserDao userdao;
    //使用注解方式是不需要有set方式
    
    public void add()
    {
        System.out.println("service.........");
        userdao.add();
    }
}

第二种方式【常用】:

使用@Resource(name="需要注入的属性的value")

@Resource(name="userdao")
    private UserDao userdao;
    //使用注解方式是不需要有set方式
    
    public void add()
    {
        System.out.println("service.........");
        userdao.add();
    }

 

4.注解与配置文件混合使用

     - bean的创建使用配置文件

     - 属性注入使用注解

1.配置文件要开启注解扫描

2.在配置文件中的定义需要被bean管理器管理的类

<?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 http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 开启注解扫描 -->    
    <!-- @param base-package 去属性值的包 扫描是否在类上 方法上 属性上 etc 是否有注解
                               如果有多个包  直接写tech.youngs 甚至tech
         -->
    <context:component-scan base-package="tech.youngs"></context:component-scan>
    
    <bean id="bookService" class="tech.youngs.XmlAno.BookService"></bean>
    <bean id="bookDao" class="tech.youngs.XmlAno.BookDao"></bean>
    <bean id="orderDao" class="tech.youngs.XmlAno.OrderDao"></bean>
</beans>

3.使用@Resource注解进行属性注入

package tech.youngs.XmlAno;

import javax.annotation.Resource;

public class BookService {
    @Resource(name="bookDao")
    private BookDao bookDao;
    
    @Resource(name="orderDao")
    private OrderDao orderDao;
    
    public void buy()
    {
        System.out.println("service...........");
        bookDao.buy();
        orderDao.buy();
    }
}

 

posted @ 2017-03-13 10:42  Youngs的学习之路  阅读(121)  评论(0编辑  收藏  举报