1、@controller 控制器(注入服务)

用于标注控制层,相当于struts中的action层

2、@service 服务(注入dao)

用于标注服务层,主要用来进行业务的逻辑处理

3、@repository(实现dao访问)

用于标注数据访问层,也可以说用于标注数据访问组件,即DAO组件

4、@component (把普通pojo实例化到spring容器中,相当于配置文件中的
<bean id="" class=""/>)

泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类

@Configuration用于定义配置类,定义的配置类可以替换xml文件,一般和@Bean注解联合使用。
如:
@Configuration
public class DemoConfiguration { @Bean
public DemoBean demoBean() {
return new DemoBean();
} }

相当于

<?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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd" default-lazy-init="false"> <bean id="demoBean" class="com.aircity.demo.entity.demoBean"></bean>
</beans>

简单就是指示一个类声明一个或多个@Bean方法,并且可以由Spring容器处理


简单看一下@Configuration的内部实现

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration { ... }
可以看到这个 @Component 注解, 意味也将会注册为bean, 其内部也可以依赖注入。 (换个方式说,一般Bean能用的东西,它也能用) 例如: @Autowired、@Inject、@Scope等
@Configuration 注释的类 类似于于一个 xml 配置文件的存在
 
  1. xml中: <bean/> 对应了Java中@Bean
  2. xml中: <context:component-scan/> 对应了Java中@ComponentScan
  3. xml中: <import/> 对应了Java中@Import
链接:https://www.jianshu.com/p/721c76c1529c

 

下面写这个是引入component的扫描组件

<context:component-scan base-package=”com.mmnc”>

其中base-package为需要扫描的包(含所有子包)

1、@Service用于标注业务层组件 
       2、@Controller用于标注控制层组件(如struts中的action) 
       3、@Repository用于标注数据访问组件,即DAO组件. 
       4、@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。    
             @Service public class UserServiceImpl implements UserService { }

@Repository public class UserDaoImpl implements UserDao { } getBean的默认名称是类名(头字母小写),如果想自定义,可以@Service(“***”)               这样来指定,这种bean默认是单例的,如果想改变,可以使用@Service(“beanName”)

@Scope(“prototype”)来改变。可以使用以下方式指定初始化方法和销毁方法(方法名任意): @PostConstruct public void init() { }

 

最新文章

  1. 安装Python算法库
  2. sql常用语句
  3. Myeclipse的使用
  4. iOS学习笔记之typedef
  5. EasyUI Layout Full - Not Correct in IE8
  6. Python 时间函数
  7. CSS 入门
  8. 3月23日html(五) 格式与布局练习:360浏览器布局
  9. 【高德地图API】从零开始学高德JS API(四)搜索服务——POI搜索|自动完成|输入提示|行政区域|交叉路口|自有数据检索
  10. java 线程的同步
  11. MySQL的&quot;旁门左道&quot;用法总结
  12. Java 第十周总结
  13. C++ 模态与非模态对话框
  14. zookeeper做集群后启动不了,大部分原因是防火墙未关闭
  15. [UE4]Tile View
  16. centos系统有多个内核,修改默认启动内核
  17. java:矩阵面积
  18. Linux配置Redis外网访问
  19. [Aaronyang] 写给自己的WPF4.5 笔记7[三巴掌-ItemsControl数据绑定详解与binding二次处理 3/3]
  20. [转载]window.location.href的用法(动态输出跳转)

热门文章

  1. BZOJ 3112 [Zjoi2013]防守战线
  2. Appium之选择/操作元素
  3. IP地址的配置
  4. 浅析Java堆,栈,方法区
  5. 解决oracle11g数据库监听连接不上问题
  6. nginx 自启动设置
  7. HTTP 错误 500.19 - Internal Server Error 错误代码 0x80070005 由于权限不足而无法读取配置文件
  8. 关于eclipse码代码时光标自动消失要重新点击输入框的问题
  9. Instrument API介绍
  10. JS基础语法---函数作为返回值使用