spring 类注解的作用
来源:互联网 发布:数据化人生 百度百科 编辑:程序博客网 时间:2024/05/16 12:01
1、@controller 控制器(注入服务)
2、@service 服务(注入dao)
3、@repository dao(实现dao访问)
4、@component (把普通pojo实例化到spring容器中,相当于配置文件中的)
@Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。
下面写这个是引入component的扫描组件
<context:component-scan base-package="com.feiniu.wyt.controller" />
<mvc:annotation-driven />
其中base-package为需要扫描的包(含所有子包)
1、@Service用于标注业务层组件
2、@Controller用于标注控制层组件(如struts中的action)
3、@Repository用于标注数据访问组件,即DAO组件.
4、@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
===================================================================
<bean id="beanname" class=""> </bean>
xml中配置 Bean 并完成 Bean 之间依赖关系的建立。Office、Car 和 Boss,这 3 个类需要在 Spring 容器中配置为 Bean
1. Office.java public clas Office{ private String officeNo = "001"; //省略 get/setter}2. Car.java public class Car{ private String brand; private String price; // 省略 get/setter }3. Boss.java public class Boss{ private Car car; private Office office; // 省略 get/setter}
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="boss" class="com.baobaotao.Boss"> <property name="car" ref="car"/> <property name="office" ref="office" /> </bean> <bean id="office" class="com.baobaotao.Office"> <property name="officeNo" value="002"/> </bean> <bean id="car" class="com.baobaotao.Car" scope="singleton"> <property name="brand" value=" 红旗 CA72"/> <property name="price" value="2000"/> </bean> </beans>
public static void main(String[] args) { String[] locations = {"beans.xml"}; ApplicationContext ctx = new ClassPathXmlApplicationContext(locations); Boss boss = (Boss) ctx.getBean("boss"); System.out.println(boss); }
@Resource @Autowire =======>>>> 自动加载bean
@Service public class UserServiceImpl implements UserService { // } @Repository public class UserDaoImpl implements UserDao { //} getBean的默认名称是类名(头字母小写),如果想自定义,可以@Service(“***”) 这样来指定,这种bean默认是单例的,如果想改变,可以使用@Service(“beanName”) @Scope(“prototype”)来改变。可以使用以下方式指定初始化方法和销毁方法(方法名任意): @PostConstructpublic void init() { }
0 0
- spring 类注解的作用
- Spring注解的作用
- Spring 注解的作用
- spring 各个注解的作用
- Spring @component 注解的作用
- Spring Mvc 的@ResponseBody注解的作用
- Spring 注解 @Scope("prototype") 的作用
- 关于Spring的Transactional注解作用范围
- Spring中Responsebody注解的作用
- Spring Boot 注解的意义以及作用
- spring中各种注解的作用
- Spring中Responsebody注解的作用
- 注解的作用与Spring的常用注解
- spring 注解@Resource,@service作用
- spring常用注解作用小结
- Spring @Autowired,@Resource,@Required注解的用法和作用
- Spring @Autowired,@Resource,@Required注解的用法和作用(转)
- Spring @Autowired,@Resource,@Required注解的用法和作用
- 深入正则表达式应用
- 只允许运行一个C#程序
- 关于冷备份与热备份的对比
- 源代码安全是软件开发企业健康发展的核心要务
- ICTCLAS 汉语词性标注集 中科院
- spring 类注解的作用
- Android真机连接Eclipse时,打不开File Explorer下的data文件夹解决方法(转)
- Android平台调用WebService详解
- 重新格式化HDFS的方法
- python-Django部署运行(二)
- windows下的PHP GD库设置
- redis cluster 管理
- Git服务器搭建和使用
- 蓝桥杯 砝码称重