spring常用注解配置
来源:互联网 发布:mysql和sql的区别 编辑:程序博客网 时间:2024/05/01 16:14
1、直接上干货:
<context:component-scan base-package="com.cmcc" /> 通过这个spring会自动扫描xxx路径下的注解。
在com.cmcc包及其子包中,如果一个类的头上带有 @Component/@Repository/@Service/@Controller 中的一个,就会将这个对象声明为bean加入容器中。@Controller 对应表现层的Bean,也就是Action;如果@Controller不指定其value【@Controller】,则默认的bean名字为这个类的类名首字母小写,
如果指定value【@Controller(value="UserAction")】或者【@Controller("UserAction")】,则使用value作为bean的名字。
@Scope struts2 是要求每次次访问都对应不同的Action, @Scope("prototype")可以保证当有请求的时候都创建一个Action对象。
@Service注解
(1)声明CmsUserService.java是一个bean
(2)CmsUserService.java在bean中的id是"cmsUserService",即类名且首字母小写。如果不想用这种形式,可以通过@Service("userService"),bean的id修改为userService
@Repository对应数据访问层Bean;@Repository(value="userDao"),但是使用mybatis,dao只定义了接口,并没有实现类,如果想把它声明了为spring容器的bean,
只有通过配置文件了。这段配置会扫描com.cmcc.akso.dao下的所有接口,然后创建各自接口的动态代理类
<!-- mybatis mapper scan --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.cmcc.akso.dao" /><property name="sqlSessionFactoryBeanName" value="sessionFactory"/></bean>
@Autowired
@Autowired 顾名思义,就是自动装配,其作用是为了消除代码Java代码里面的getter/setter与bean属性中的property。
默认按类型匹配的方式,在容器查找匹配的Bean,当有且仅有一个匹配的Bean时,Spring将其注入 @Autowired标注的变量中。
如果属性找不到我不想让Spring容器抛出异常,就是显示null,将 @Autowired注解的required属性设置为false即可: @Autowired(required=false)如果一个接口有多个实现类,可以用 @Qualifier来指定bean的名称
@Autowired@Qualifier("cmsUserService")private UserService cmsUserservice;(1)@Resource后面没有任何内容,默认通过name属性去匹配bean,找不到再按type去匹配
(2)指定了name或者type则根据指定的类型去匹配bean
两个注解的区别:
(1)@Autowired默认按照byType方式进行bean匹配,@Resource默认按照byName方式进行bean匹配
(2)@Autowired是Spring的注解,@Resource是J2EE的注解,Spring属于第三方的,J2EE是Java自己的东西,因此,建议使用@Resource注解,以减少代码和Spring之间的耦合。
@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
2、汇总:
@Scope注解 作用域@Lazy(true) 表示延迟初始化
@Service用于标注业务层组件、
@Controller用于标注控制层组件(如struts中的action)
@Repository用于标注数据访问组件,即DAO组件。
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。不建议使用。
@Scope用于指定scope作用域的(用在类上)
@PostConstruct用于指定初始化方法(用在方法上)
@PreDestory用于指定销毁方法(用在方法上)
@DependsOn:定义Bean初始化及销毁时的顺序
@Primary:自动装配时当出现多个Bean候选者时,被注解为@Primary的Bean将作为首选者,否则将抛出异常
@Autowired 默认按类型装配,如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。如下:
@Autowired @Qualifier("personDaoBean") 存在多个实例配合使用
@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。
@PostConstruct 初始化注解
@PreDestroy 摧毁注解 默认 单例 启动就加载
@Async异步方法调用
3、@Value("${memercached.servers}")这种形式不需要指定具体加载对象,这时候需要一个关键的对象来完成
<!-- 定义受环境影响易变的变量 --><beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /><property name="ignoreResourceNotFound" value="false" /><property name="locations"><list><value>classpath*:/config.properties</value> </list></property></bean>config.properties文档中添加
memercached.servers=${env.memercached.servers}
env.memercached.servers配置在pom.xml文件里
自己在写项目中,发现有时@Value无法取到config.properties文件里的值,经排查,以上bean组件需要在spring-mvc.xml和spring.xml各配置一份
0 0
- spring常用注解配置
- Spring常用注解、配置整理
- 【spring ----- 注解篇】 常用注解
- spring常用注解
- Spring IOC 常用注解
- Spring常用注解方式
- Spring常用注解
- spring常用注解
- spring 常用注解集合
- Spring IOC 常用注解
- spring常用注解
- Spring常用注解
- Spring IoC常用注解
- spring常用注解
- Spring常用注解
- Spring常用注解
- spring mvc 常用注解
- spring常用注解
- 心跳检测的思路及代码
- Swift 看着课本学Swift第一课
- Android驱动开发流程 及 系统分层模型
- linux下文件的三个时间(Atime ,Mtime,Ctime)
- Codeforces Round #355 (Div. 2) B. Vanya and Food Processor (模拟)
- spring常用注解配置
- C++字符串
- OpenGL、OpenGL ES、OpenVG、GLX、EGL简介
- 关于storybord加视图控制器的代码实现方式
- mysql 数据库通过命令导出数据库文件
- Python读取和写入Excel文件[整]
- 常用的http方法和http响应码详解
- OpenCV使用霍夫变换检测图像中的形状
- Codeforces Round #368 (Div. 2)