Spring自带的@Component注解及扩展
@Component:定义Spring管理Bean
@AspectJ风格的切面可以通过@Compenent注解标识其为Spring管理Bean,而@Aspect注解不能被Spring自动识别并注册为Bean,必须通过@Component注解来完成
@Component @Aspect public class TestAspect { @Pointcut(value="execution(* *(..))") private void pointcut() {} @Before(value="pointcut()") public void before() { System.out.println("=======before"); } }
通过@Component将切面定义为Spring管理Bean。
@Repository:
@Component扩展,被@Repository注解的POJO类表示DAO层实现,从而见到该注解就想到DAO层实现,使用方式和@Component相同;
@Service:
@Component扩展,被@Service注解的POJO类表示Service层实现,从而见到该注解就想到Service层实现,使用方式和@Component相同;
@Controller:
@Component扩展,被@Controller注解的类表示Web层实现,从而见到该注解就想到Web层实现,使用方式和@Component相同;
在使用Spring代理时,默认只有在public可见度的方法的@Transactional 注解才是有效的,其它可见度(protected、private、包可见)的方法上即使有@Transactional 注解也不会应用这些事务属性的,Spring也不会报错,如果你非要使用非公共方法注解事务管理的话,可考虑使用AspectJ。
Spring声明式事务实现其实就是Spring AOP+线程绑定实现,利用AOP实现开启和关闭事务,利用线程绑定(ThreadLocal)实现跨越多个方法实现事务传播。
相关推荐
Spring Boot技术知识点:如何深入理解@Component注解
package soundsystem; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired;...import org.springframework....
Spring @Component 注解原理解析 Spring 框架中 @Component 注解是用于标注 Bean 的一种方式,通过使用 @Component 注解,Spring 框架可以自动将该类纳入到容器中管理,从而实现了依赖注入和控制反转。 @Component...
Spring 注解@Component、@Repository、@Service、@Controller 区别 在 Spring 框架中,@Component、@Repository、@Service、@Controller 是四个常用的注解,它们都是继承自 @Component 注解,用于标注不同的组件或 ...
使用@ConfigurationProperties和@Component注解到bean定义类上,这里@Component代指同一类实例化Bean的注解。 1.3 场景二 使用@ConfigurationProperties和@Bean注解在配置类的Bean定义方法上。以数据源配置为例: ...
@Component 注解是所有受 Spring 管理组件的通用形式,默认的名称(id)是小写开头的非限定类名。可以使用 @Component("abc") 指定 Bean 的名称。 2. @Repository @Repository 注解用于将数据访问层 (DAO 层) 的类...
在上面的示例代码中,我们可以看到,使用@Configuration注解的MyBeanConfig类和使用@Component注解的MyBeanConfig类都可以生成Country和UserInfo实例,但是它们之间存在着一些关键的区别。当我们使用@Configuration...
项目分层之后(引入dao,service,web层之后), @Component注解还有三个分身---@repository ,@Service,@Controller。这三个注解怎么用,以后再说,目前都使用@Component。 1.1 为Car类加@Component注解 注解也是要用...
@Component注解和@Configuration注解的区别在于,@Component注解只是简单地标记一个组件类,而@Configuration注解则标记一个配置类。配置类需要被动态代理,以便生成Bean容器中的Bean对象,而组件类则不需要动态代理...
5. 使用 @Bean 注解与 @Component 或 @Configuration 一起使用。在使用 @Bean 注解时,可以与 @Component 或 @Configuration 一起使用,用于注册 Bean 实例到 Spring 容器中。例如: ```java @Configuration public...
@Component注解是Spring框架中的一种注解,用于描述Spring中的Bean,标注在某个类上,表示该类是一个组件(Bean)。@Component注解是一个泛化的概念,可以作用在任何层次。 四、其他常用的注解 除了@Configuration...
我们需要将自定义四个注解,然后将Group和User类使用@Component注解,在User类中创建Group类的实例化对象并设置为自动装配,这样就能在User类中调用Group类的方法; 然后我们需要自己实现一个IoC容器类,处理自定义...
Spring注解 @Component、@Repository、@Service、@Controller区别,有兴趣的可能看一下。
Spring作为组件的接口该库在接口上添加了对Spring的@Component注释的支持: @Componentpublic interface Dummy { void dummy ();} 对于这样的接口,框架构建了一个代理对象,该代理对象将方法调用路由到方法处理程序...
@Component 在类定义之前添加@Component注解,他会被spring容器识别,并转为bean。 @Repository 对Dao实现类进行注解 (特殊的@Component) @Service 用于对业务逻辑层进行注解, (特殊的@Compone
@Component 注解表示当前类是一个组件类,SpringBoot 将扫描该类并将其注册到应用程序的上下文中。 3. @RestController @RestController 注解是一个组合注解,相当于 @Controller 和 @ResponseBody 的合集。它表示...
在 Spring Boot 中,@Scheduled 注解可以与 @Component 注解组合使用,以便将定时任务 Bean 注册到 Spring IoC 容器中。在以下代码中,我们可以看到一个简单的定时任务示例: ```java @Component public class ...
在这个例子中,我们使用了@Component注解标记Zoo类为一个Spring管理的bean,然后在构造函数上使用@Autowired注解。Spring会自动找到Tiger和Monkey类型的bean并注入到构造函数中,无需在.xml文件中进行任何配置。 ...
Spring boot @Aspect简介使用场景列子1 (演示基本过程切点表达式切点复合运算切点...@Component public class TestAspect { /** * 声明一个切入点, 命名 pointcut1 * */ @Pointcut("execution(public java.util.M
Spring Boot 使用 Value 注解给静态变量...使用 Spring Boot 的 @Value 注解来给静态变量赋值需要使用非静态的 set 方法来解决问题,我们可以通过添加 @Component 注解和使用非静态的 set 方法来实现静态变量的赋值。