8.Spring IOC注解

目录

  1. 注解的作用
  2. 有哪些注解
  3. 注解的使用
  4. 实例

 

一.注解的作用

基于XML的bean属性配置:bean的定义信息与bean的实现类是分离的。

基于注解的配置:bean的定义信息是通过在bean实现类上标注注解实现。

也就是说,加了注解,相当于在XML中配置了

 

二.有哪些注解

Spring几种常用注解:

  1. 被@Component注解的POJO类将自动被Spring识别并注册到Spring容器中,且自动支持自动装配。
  2. 被@Repository注解的POJO类表示DAO层实现
  3. 被@Service注解的POJO类表示Service层实现,从而见到该注解就想到Service层实现,使用方式和@Component相同;
  4. 被@Controller注解的类表示Web层实现,从而见到该注解就想到Web层实现,使用方式和@Component相同;

    要点:
    1. 都可以通过spring的getBean形式得到注解类:如下:
      ctx.getBean("component", TestCompoment.class);  
    2. .@Controller、@Service、@Repository是在Spring自带的@Component基础上拓展而来,它们分别对应表现层Bean、业务层Bean 和数据层Bean,他们除了语义上不同之外,本质和用法上没有什么区别。而@Component以过时不推荐使用。
  5. @Resource其实与@Autowired的作用相似,都是代替<property>标签来注入对象的,但推荐使用@Resource
    注解@Resource与@Autowired的区别
  6. @Scope是用来设定Bean的生命周期的。
  7. 除了上述常用的之外,其他的还有@Lazy、@DependsOn、@PostConstruct、@PreDestroy等其他的注解,作用分别是:延迟初始化、依赖其他Bea、初始化方法、析构方法。

注解放置的位置:

  1. 注解@标识放置的代码位置不是随意的,根据jdk1.5规范,要求注解写在类、接口、属性(成员变量)、方法、构造函数或方法参数上。不同的注解,可放置的位置也不同。

    例如上述:
    @Controller、@Service、@Repository、@Scope、@Lazy、@DependsOn都只能放在类上;
    @Resource、@Autowired能放在属性、方法、构造函数上;
    @PostConstruct、@PreDestroy则只能放在方法上

 

三.注解的使用

要想使用Spring注解,必须要在applicationContext.xml中配置<context:annotation-config>和<context:component-scan>这两个标签,前者用来告诉Spring要启用注解,后者则用来告诉Spring注解所在的包,以便Spring启动时扫描包并进行注入。

在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:annotation-config></context:annotation-config>
   <context:component-scan base-package="com"/>
</beans>

说明:context:component-scan :扫描这个包中的所有类,并从注解信息中获取bean的基本信息(没加注解的不扫描也不实例化)。

表达式过滤:context:component-scan的配置直接选了相应包里的全部class,可以使用过滤

<context:component-scan base-package="com">
       <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Component"/>
</context:component-scan>

说明:

include-filter表示:包含哪些。type是类型,expression是表达式过滤。
exclude-filter表示:除去哪些。type是类型,expression是表达式过滤。

如:

 

四.实例

实例1

对于扫描到的组件
方法1.使用非限定类名,第一个字母小写(例:Person类,beanname=person)(默认,如果没有value属性)

方法2.使用value属性标识组件名称

 

实例2

1.结构目录

2.代码

Person.java(使用value属性标识组件名称)

package com;

import org.springframework.stereotype.Component;

@Component(value="person1")
public class Person {
	public void test() {
		System.out.println("用户信息");
	}
	
}

Test.java

package test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.Person;

public class Test {

	public static void main(String[] args) {
		ConfigurableApplicationContext bf=new ClassPathXmlApplicationContext("beans.xml");
		Person per=(Person) bf.getBean("person1");
		
		per.test();	
	}
}

配置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"/>
</beans>

运行Test.java ,会在打印台显示

用户信息

3.结论

在Person类中加了注解@Component,spring看到这个属性标志,会自动将Person变成容器管理类,等同于在XML中这样配置:

 <bean id="person" class="com.Person"></bean>

 

实例3

使用到的注解有:@Controller 、@Service、@Repository  、自动配置注解@Autowired 、@Resource

1.目录

2.代码

1.StudentController.java

package com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.service.StudentService;

@Controller
public class StudentController {
	
	@Autowired
	private StudentService stuService;
	
	public String add(){
		System.out.println("我是控制层");
		stuService.add("李四四");
		return "add";
	}

	public void setStuService(StudentService stuService) {
		this.stuService = stuService;
	}
	
	
}

2.StudentDao.java(接口类)

 

package com.dao;

/*
 * 持久化数据层
 */
public interface StudentDao {

	public boolean add(String name);

}

3.StudentDaoImpl.java(接口实现类)

 

package com.dao.impl;

import org.springframework.stereotype.Repository;

import com.dao.StudentDao;

@Repository
public class StudentDaoImpl implements StudentDao{

	@Override
	public boolean add(String name) {
		System.out.println("我是持久化层添加数据:"+name);
		return false;
	}

}

4.StudentService.java(接口类)

 

package com.service;

/*
 * 业务逻辑层
 */
public interface StudentService {
 
	public boolean add(String name);
}

5.StudentServiceImpl.java(接口实现类)

 

package com.service.impl;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dao.StudentDao;
import com.service.StudentService;

@Service
public class StudentServiceImpl implements StudentService{

	@Resource
	private StudentDao dao;
	
	@Override
	public boolean add(String name) {
		System.out.println("我是业务逻辑层:"+name);
		dao.add("张三1");
		return false;
	}

	public void setDao(StudentDao dao) {
		this.dao = dao;
	}

	
}

6.Person.java

 

package com;

import org.springframework.stereotype.Component;

@Component(value="person1")
public class Person {
	public void test() {
		System.out.println("用户信息");
	}
	
}

7.Test.java

 

package test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.Person;
import com.controller.StudentController;
import com.dao.impl.StudentDaoImpl;
import com.service.impl.StudentServiceImpl;

public class Test {

	public static void main(String[] args) {
		ConfigurableApplicationContext bf=new ClassPathXmlApplicationContext("beans.xml");
		
		//Person类
		Person pe=(Person)bf.getBean("person1");
		pe.test();
		
		//持久化数据层(dao)
		StudentDaoImpl sd=(StudentDaoImpl)bf.getBean("studentDaoImpl");
		sd.add("张三");
		
		//业务逻辑层(service)
		StudentServiceImpl ss=(StudentServiceImpl)bf.getBean("studentServiceImpl");
		ss.add("李四");
		
		//控制层(controller)
		StudentController sc=(StudentController)bf.getBean("studentController");
		sc.add();
		
	}
}

8.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">
   		<!-- 去除controller包下的StudentController类,既去除注解
   		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
   		 -->
   </context:component-scan>
</beans>

结果:运行Test.java 控制台打印

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值