spring注解学习第一天(打卡)

@configuration:

@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContextAnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。

示例:

package com.spring_annotation.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;

import com.spring_annotation.pojo.Student;

@Configuration
@ComponentScan(value = "com.spring_annotation",includeFilters = {
		@Filter(type=FilterType.CUSTOM,value= {MyFilter.class})
},useDefaultFilters = false)
public class MainConfig {
	
	@Bean("Student")
	public Student Student() {
		return new Student("李四",55);
	}
}

测试

package com.spring_annotation.spring_annotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring_annotation.config.MainConfig;
import com.spring_annotation.pojo.Student;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        
        //注解方式
        
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);

        
        Student student = (Student) applicationContext.getBean("Student");
        System.out.println(student.toString());
    }
}

@Bean:

标注在方法上(返回某个实例的方法),等价于spring的xml配置文件中的,作用为:注册bean对象

以及自定义TypeFilter指定过滤规则

package com.spring_annotation.config;

import java.io.IOException;

import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

public class MyFilter implements TypeFilter{

	@Override
	public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
			throws IOException {
		// TODO Auto-generated method stub
		
		AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
		
		if(annotationMetadata.getClassName().contains("er")) {
			return true;
		}
		return false;
	}

}

@Scope

对象在spring容器(IOC容器)中的生命周期,也可以理解为对象在spring容器中的创建方式。

默认是单例模式,即scope="singleton"。

1.singleton:单例模式-全局有且仅有一个实例

2.prototype:原型模式-每次获取Bean的时候会有一个新的实例

3.request:request表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP request内有效,

4.session:session作用域表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP session内有效

5.global session:global session作用域类似于标准的HTTP Session作用域,不过它仅仅在基于portlet的web应用中才有意义。Portlet规范定义了全局Session的概念,它被所有构成某个 portlet web应用的各种不同的portlet所共享。在global session作用域中定义的bean被限定于全局portlet Session的生命周期范围内。如果你在web中使用global session作用域来标识bean,那么web会自动当成session类型来使用。

详解

@ComponentScan

默认扫描其注解的类所在的包和子包下面所有的注解。

其他相关代码:

 

package com.spring_annotation.pojo;

public class Student {
	private String name;
	private Integer age;
	
	public Student() {
		super();
	}
	
	public Student(String name, Integer age) {
		super();
		this.name = name;
		this.age = age;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
	
	
}

附:

  • @Component 是一个泛化的概念,仅仅表示一个组件 (Bean) ,可以作用在任何层次。
  • @Service 通常作用在业务层,但是目前该功能与 @Component 相同。
  • @Constroller 通常作用在控制层,但是目前该功能与 @Component 相同。
  • @Repository 同于Dao层这是因为该注解的作用不只是将类识别为Bean,同时它还能将所标注的类中抛出的数据访问异常封装为 Spring 的数据访问异常类型。 Spring本身提供了一个丰富的并且是与具体的数据访问技术无关的数据访问异常结构,用于封装不同的持久层框架抛出的异常,使得异常独立于底层的框架。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值