常用注解解析(因为不太明白@component和@configuration写了)

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() { }

 

猜你喜欢

转载自www.cnblogs.com/share-record/p/11963902.html
今日推荐