spring2.5注解加xml开发SSM框架
基础使用
SSM三层式开发方式:
注册为Bean组件;
@Controller:控制层
@Service:业务逻辑层;
@Repository :数据访问层;
@Component:非这以上三层;
1
2
3
4
5<?xml version="1.0" encoding="UTF-8"?> <beans 很多东西xxxx> <context:component-scan base-package="包路径"></context:component-scan> </beans>
1、注解必须在类上,不能再接口上,实体类一般不注册为bean,并且默认beanName是类的首字母小写方式注入;
2、这四种注解底层都是一样的,都跟Component一摸一样。便于识别和管理;
例子:排除Controller注解,不去扫描
exclude-filter:设置需要排除的选项
include-filter:设置包含
type
annotation :根据注解的完全限定名
assignable:根据类的完整限定名
aspectj:根据切面表达式来排除的,一般不使用
regex:根据正则表达式
custom:自定义,根据接口设置排除 用的很少
use-default-filters:默认true:会默认包含扫描@Controller @Service @Repository @Component
false:不会扫描@Controller @Service @Repository @Component
1
2
3
4
5
6<!--包下Controller注解都不能被识别--> <context:component-scan base-package="包路径"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"> </context:exclude-filter> </context:component-scan>
注入
1、硬编码
1
2
3@Value("管理员") private String name;
2、${},#{}
引用外部文件属性
1
2<context:property-placeholder location="db.properties"></context:property-placeholder>
db.properties文件中写上mysql.username = root
1
2
3@Value("${mysql.username}") private String name;
获取bean中的属性
1
2
3
4
5
6
7//A类 @Value("管理员") private String name; //在B类中的引用 @Value("#{A.name}") private String name;
@Autowired自动装配
1、默认优先根据类型去匹配,再根据名字匹配
1
2
3
4@Autowired UserService userServiceImpl; //默认先根据UserService来匹配,如果ioc中存在多个就会根据名字来匹配userServiceImpl
1
2
3
4
5
6
7
8
9
10
11①修改发name匹配实现类 ②@Service("修改后的名字")或者@Service(value="修改后的名字"),在@Autowired下面获取时 UserService 修改后的名字; ③@Qualifier(”去匹配实现类名“) @Autowired @Qualifier(”去匹配实现类名“) UserService 随便什么名; ④@Primary 设置主要注入的bean
1
2
3
4
5
6
7
8@Service //userServiceImpl @Primary //设置自动注入的主要Bean public class UserServiceImpl implements UserService{ } }
⑤公共接口,泛型
T:可以时任何类型
1
2
3
4
5
6
7
8@Component public interface BaseService<T>{ T getBean(); //公共的增删查改 } BaseService userService; BaseService user2Service;
控制加载顺序
bean的加载顺序默认是文件排序从上到下加载的,
B类加@Deprecated(“a”),那么a类在B类之前加载;
懒加载:@Lazy
多例:@Scope:默认@Scope(“sington”)还是单例
@Scope(“prototype”);多例
生命周期回调
1
2
3
4
5
6
7
8
9
10
11
12
13
14// 生命周期回调-3 初始化回调 @PostConstruct public void init(){ System.out.println("初始化"); } // 生命周期回调-3 销毁回调 @PreDestroy public void destory(){ System.out.println("销毁"); }
在spring2.5第三方bean还是跟之前一样没办法用注解
Spring3.0基于JavaConfig的配置
新建类IoCJavaConfig.java
@Configuration:标记一个java配置类。相当于以前的的xml配置类;
@ComponentScan(basePackages = “cn.xxxx”):指定扫描的包路径
1
2
3
4
5
6
7
8
9@Configuration @ComponentScan(basePackages = "cn.xxxx") @PropertySource("classpath:db.properties") @Import(IoCJavaConfig2.class)//引入其他的配置类 public class IoCJavaConfig { } //加载spring上下文 AnnotationConfigApplicationContext ioc=new AnnotationConfigApplicationContext(IoCJavaConfig.class);
配置第三方bean:(自己的bean想这样配置肯定也是没问题的,并且替换原有spring自己注册的替换掉)
引用外部属性;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31//在IoCJavaConfig 上面加上@PropertySource("classpath:db.properties")引用外部属性 @Value("${mysql.username}") private String name; @Value("${mysql.password}}") private String password; @Value("${mysql.url}") private String url; @Value("${mysql.driverClassName}") private String driverClassName; //@Bean(initMethod="",destryMethod="") 生命周期回调 @Bean(name = {"dataSource","dd"})//name 设置名称,可以设置多个,这边设置了两个 //@Scope("prototype") 作用域也可以设置,多例,单例 public DruidDataSource dataSource(Role role){ //自动依赖外部bean //这边直接把Role 传进来就是引用其他已经注入了Spring的bean,非常简单好用 DruidDataSource dataSource=new DruidDataSource(); dataSource.setName(name); dataSource.setPassword(password); dataSource.setUrl(url); dataSource.setDriverClassName(driverClassName); System.out.println(user3()); //依赖内部bean(写在同一个配置类里面的bean就是内部bean) return dataSource; } @Bean public User user3(){ return new User(); }
知识点:
1、配置第三方bean,直接@Bean
1
2
3@Bean public DruidDataSource dataSource(){}
2、引用外部属性
类上面写上@PropertySource(“classpath:db.properties”);
3、依赖外部bean Role
1
2
3public DruidDataSource dataSource(Role role){ }
4、依赖内部bean直接调用方法user3();
5、①@Import(IoCJavaConfig2.class) 引入其他的配置类
②@Import(User.class) 也可以把类注册为一个bean
③实现ImportSelector接口重写selectImports方法,返回多个bean(框架级别的使用,以后很重要)
1
2
3
4
5
6
7
8
9
10
11
12
13
14@Import(MyImportSelector.class) public class IoCJavaConfig { } @Component public class MyImportSelector implements ImportSelector { public String[] selectImports(AnnotationMetadata importingClassMetadata) { // 可以以字符串的形式注册多个Bean // 字符串必须是类的完整限定名 getBean不能根据名字获取获取的, 必须要根据类型获取 return new String[]{"cn.xxxx.beans.Person",Wife.class.getName()}; // return new String[]{"cn.xxxx.beans.Person","cn.xxxx.beans.Wifi"};两种都行 } }
1
2④实现ImportBeanDefinitionRegistrar接口,重写registerBeanDefinitions方法;也可以注册多个bean
1
2
3
4
5
6
7
8
9
10
11
12
13
14@Import(MyImportBeanDefinitionRegistrar.class) public class IoCJavaConfig { } @Component public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { GenericBeanDefinition beanDefinition=new GenericBeanDefinition(); beanDefinition.setBeanClass(Person.class); //直接通过person名字就可以了 registry.registerBeanDefinition("person",beanDefinition); } }
最后
以上就是单纯身影最近收集整理的关于spring 2.5,3.0配置的全部内容,更多相关spring内容请搜索靠谱客的其他文章。
发表评论 取消回复