spring注解第一课
Posted 超轶绝尘
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring注解第一课相关的知识,希望对你有一定的参考价值。
一、原始的 xml配置方式
1.Spring pom 依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.2.RELEASE</version> </dependency>
2.JavaBean
public class Person { private String name; private Integer age; public Person() { } public Person(String name, Integer age) { 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 "Person [name=" + name + ", age=" + age + "]"; } }
3.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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="person" class="com.atguigu.bean.Person"> <property name="name" value="张三" /> <property name="age" value="10" /> </bean> </beans>
4.测试类
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.atguigu.bean.Person; public class Test { public static void main(String[] args) { ClassPathXmlApplicationContext beans = new ClassPathXmlApplicationContext("beans.xml"); Person p = (Person)beans.getBean("person"); System.out.println(p); } }
二、注解的形式
1. @Configuration替代beans.xml,@bean 替代<bean>
package com.atguigu.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.ComponentScans; import com.atguigu.bean.Person; //配置类==配置文件 @Configuration //告诉Spring这是一个配置类 @ComponentScans( value = { @ComponentScan(value="com.atguigu",includeFilters = { /* @Filter(type=FilterType.ANNOTATION,classes={Controller.class}), @Filter(type=FilterType.ASSIGNABLE_TYPE,classes={BookService.class}),*/ @Filter(type=FilterType.CUSTOM,classes={MyTypeFilter.class}) },useDefaultFilters = false) } ) //@ComponentScan value:指定要扫描的包 //excludeFilters = Filter[] :指定扫描的时候按照什么规则排除那些组件 //includeFilters = Filter[] :指定扫描的时候只需要包含哪些组件 //FilterType.ANNOTATION:按照注解 //FilterType.ASSIGNABLE_TYPE:按照给定的类型; //FilterType.ASPECTJ:使用ASPECTJ表达式 //FilterType.REGEX:使用正则指定 //FilterType.CUSTOM:使用自定义规则 public class MainConfig { //给容器中注册一个Bean;类型为返回值的类型,id默认是用方法名作为id @Bean("person") public Person person01(){ return new Person("lisi", 20); } }
2.测试用例
package com.atguigu; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.atguigu.bean.Person; import com.atguigu.config.MainConfig; public class MainTest { @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class); Person bean = applicationContext.getBean(Person.class); System.out.println(bean); String[] namesForType = applicationContext.getBeanNamesForType(Person.class); for (String name : namesForType) { System.out.println(name); } } }
以上是关于spring注解第一课的主要内容,如果未能解决你的问题,请参考以下文章