`
liyixing1
  • 浏览: 967114 次
  • 性别: Icon_minigender_1
  • 来自: 江西上饶
社区版块
存档分类
最新评论

注解 简化bean配置

阅读更多
?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
              
     <context:annotation-config/>
    
</beans>

(隐式注册 post-processors 包括了 AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,也包括了前面提到的 RequiredAnnotationBeanPostProcessor。)
@Autowired

@Autowired 注解可以用于“传统的”setter 方法,如下例:

public class SimpleMovieLister {

    private MovieFinder movieFinder;

    @Autowired
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }

    // ...
}
这个注解也可以用于以属性为参数/多个参数的方法

public class MovieRecommender {

    private MovieCatalog movieCatalog;
   
    private CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    public void prepare(MovieCatalog movieCatalog, CustomerPreferenceDao customerPreferenceDao) {
        this.movieCatalog = movieCatalog;
        this.customerPreferenceDao = customerPreferenceDao;
    }

    // ...
}
prepare这个方法添加了自动拼装,因此spring在初始化这个bean的时候会调用这个方法。

@Autowired注解甚至可以用于构造器与字段:

public class MovieRecommender {

    @Autowired
    private MovieCatalog movieCatalog;
   
    private CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
        this.customerPreferenceDao = customerPreferenceDao;
    }

    // ...
}
也可以一种提供来自ApplicationContext的特殊类型的所有 beans,注解字段或者方法,例如:

public class MovieRecommender {

    @Autowired
    private MovieCatalog[] movieCatalogs;

    // ...
}
这同样适用于集合类型:

public class MovieRecommender {

    private Set<MovieCatalog> movieCatalogs;
   
    @Autowired
    public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
        this.movieCatalogs = movieCatalogs;
    }

    // ...
}
甚至是 Maps 也可以这样注解,只要这个 Map 的 key 类型为 String。这个 Map 的 values 应该是已知的类型,并且 keys 应该包含符合 bean 的命名:

public class MovieRecommender {

    private Map<String, MovieCatalog> movieCatalogs;
   
    @Autowired
    public void setMovieCatalogs(Map<String, MovieCatalog> movieCatalogs) {
        this.movieCatalogs = movieCatalogs;
    }

    // ...
}
在缺省情况下,当出现0个候选的 beans时自动连接将失败;缺省行为把连接方法,构造器,字段假设为 required 的依赖。这样的行为如下所示:

public class SimpleMovieLister {

    private MovieFinder movieFinder;

    @Autowired(required=false)
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }

    // ...
}
Note
虽然当 一个类只有一个连接构造器时它将被标记为 required, 但是还是可以标记多个构造器的。在这种情况下,每一个构造器都有可能被认为是连接构造器, Spring 将会把依赖关系能够满足的构造器认为是greediest 的构造器。

@Autowired也能用于总所周知的“可解决的依赖”:BeanFactory接口,ApplicationContext接口,ResourceLoader接口,ApplicationEventPublisher接口,还有MessageSource接口。这些接口(还有它们的扩展,例如ConfigurableApplicationContext或者ResourcePatternResolver)将可以自动解决依赖,没有任何特殊必须的其它步骤需要。

public class MovieRecommender {

    @Autowired
    private ApplicationContext context;

    public MovieRecommender() {
    }

    // ...
}

另外自动注解
标注@Autowired注解的Bean并不会自动进行装配,它需要一个配套的处理器,既AutowiredAnnotationBeanPostProcessor,该Bean后置处理器会在Spring容器启动时自动为标住@Autowired注解的Bean实施自动装配.
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
或者使用
<context:annotation-config />代替。它将自动完成注解配置的依赖配置
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!--
<bean id="accountService"
class="com.liyixing.spring.service.imp.AccountService"> <lookup-method
name="look" bean="downloadService" /> </bean> <bean
id="downloadService"
class="com.liyixing.spring.service.imp.DownloadService"
scope="prototype"> </bean> <bean id="messageService"
class="com.liyixing.spring.service.imp.MessageService"> </bean>
<context:annotation-config />
--><context:annotation-config />
</beans>

Test.java
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "beans.xml" });
System.out.println(context.getBeanDefinitionCount());
String[] beanNames = context.getBeanDefinitionNames();

for(String beanName : beanNames) {
System.out.println(beanName);
}
可以看到结果是



自动定义了依赖的bean

基于注解的自动连接微调
因为通过类型的自动连接可能会有多个候选,因此经常需要在选择过程中加以控制。一种方法去完成这个控制就是使用@Qualifier注解。在最简单的情况下,您能够通过命名方式去实现这个自动连接:

public class MovieRecommender {

    @Autowired
    @Qualifier("mainCatalog")
    private MovieCatalog movieCatalog;

    // ...
}
@Qualifier注解也能够被指定为构造器的参数或者方法的参数:

public class MovieRecommender {

    private MovieCatalog movieCatalog;
   
    private CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    public void prepare(@Qualifier("mainCatalog") MovieCatalog movieCatalog, CustomerPreferenceDao customerPreferenceDao) {
        this.movieCatalog = movieCatalog;
        this.customerPreferenceDao = customerPreferenceDao;
    }

    // ...
}
您也可以创建您自定义的限定器注解。您只要在定义一个注解时提供@Qualifier注解就可以了:

@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Genre {

    String value();

}
然后您就能够将这个自定义的限定器与参数用于自动连接的字段:

public class MovieRecommender {

    @Autowired
    @Genre("Action")
    private MovieCatalog actionCatalog;
   
    private MovieCatalog comedyCatalog;
   
    @Autowired
    public void setComedyCatalog(@Genre("Comedy") MovieCatalog comedyCatalog) {
        this.comedyCatalog = comedyCatalog;
    }

    // ...
}
下一步就是提供信息给候选的 bean 的定义。您能够添加<qualifier/>标签作为<bean/>标签的子元素,然后指定'type'还有'value'以匹配您的自定义限定器注解。类型必须匹配注解的全名,或者是一个不危险的、方便一点的名字,您也可以使用“短” 类名。参看下例:

<?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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config/>

    <bean class="example.SimpleMovieCatalog">
        <qualifier type="Genre" value="Action"/>
        <!-- inject any dependencies required by this bean -->
    </bean>

    <bean class="example.SimpleMovieCatalog">
        <qualifier type="example.Genre" value="Comedy"/>
        <!-- inject any dependencies required by this bean -->
    </bean>
   
    <bean id="movieRecommender" class="example.MovieRecommender"/>

</beans>

在某些情况下,有足够充分的理由去使用不带值的注解。这使得注解可以提供更多解决不同类型依赖的能力。例如,在 Internet 连接不可用时,您可以提供一个离线的搜索目录。首先就要定义一个简单的注解:

@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Offline {

}
然后添加这个注解给字段作为自动连接:

public class MovieRecommender {

    @Autowired
    @Offline
    private MovieCatalog offlineCatalog;

    // ...
}
现在,这个 bean 的定影只组要一个限定器了:

<bean class="example.SimpleMovieCatalog">
    <qualifier type="Offline"/>
    <!-- inject any dependencies required by this bean -->
</bean>
另外,也可以定制自己的限定器注解去使用命名的属性或者简单的'value'属性。如果自动连接时多个属性值被指定给了一个字段或者参数,那么一个 bean 的定义必须全部匹配这些属性的值。例如,考虑如下的注解定义:

@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface MovieQualifier {

    String genre();
   
    Format format();

}
在这种情况下,Format是一个枚举:

public enum Format {
   
    VHS, DVD, BLURAY
}
这些字段将与自定义的限定器进行自动连接,包括了每个属性的值:'genre' 以及 'format'。

public class MovieRecommender {

    @Autowired
    @MovieQualifier(format=Format.VHS, genre="Action")
    private MovieCatalog actionVhsCatalog;

    @Autowired
    @MovieQualifier(format=Format.VHS, genre="Comedy")
    private MovieCatalog comedyVhsCatalog;

    @Autowired
    @MovieQualifier(format=Format.DVD, genre="Action")
    private MovieCatalog actionDvdCatalog;

    @Autowired
    @MovieQualifier(format=Format.BLURAY, genre="Comedy")
    private MovieCatalog comedyBluRayCatalog;
  
    // ...
}
最终,这个 bean 的定义应该与限定器匹配的值。这个列子将说明bean 的元属性可以用于替代<qualifier/>的子元素。那样的话,<qualifier/>以及它的属性将优先考虑,但是如果没有限定器的话(参看如下定义的后两个 bean ),自动连接机制将取消以<meta/>标签标记的值。

<?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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config/>

    <bean class="example.SimpleMovieCatalog">
        <qualifier type="MovieQualifier">
            <attribute name="format" value="VHS"/>
            <attribute name="genre" value="Action"/>
        </qualifier>
        <!-- inject any dependencies required by this bean -->
    </bean>

    <bean class="example.SimpleMovieCatalog">
        <qualifier type="MovieQualifier">
            <attribute name="format" value="VHS"/>
            <attribute name="genre" value="Comedy"/>
        </qualifier>
        <!-- inject any dependencies required by this bean -->
    </bean>

    <bean class="example.SimpleMovieCatalog">
        <meta key="format" value="DVD"/>
        <meta key="genre" value="Action"/>
        <!-- inject any dependencies required by this bean -->
    </bean>
   
    <bean class="example.SimpleMovieCatalog">
        <meta key="format" value="BLURAY"/>
        <meta key="genre" value="Comedy"/>
        <!-- inject any dependencies required by this bean -->
    </bean>

</beans>

组件化的注解@Component
spring2.0开始支持@Repository注解,标记用来充当库存。包括了@Repository,@Service和@Controller。而这三个注解则是@Component的细化。分别对应了持久层(@Repository),服务层(@Service),控制层或叫表现层(@Controller)。选择使用细化的注解而不是使用@Component则更好。
如:
@Service
public class SimpleMovieLister {

    private MovieFinder movieFinder;

    @Autowired
    public SimpleMovieLister(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }
}

@Repository
public class JpaMovieFinder implements MovieFinder {
    // implementation elided for clarity
}

检测这些类并注册相应的bean,需要在XML中包含以下元素,其中'basePackage'是两个类的公共父包 (或者可以用逗号分隔的列表来分别指定包含各个类的包)。
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
              
     <context:component-scan base-package="org.example"/>
    
</beans>

此外,在使用组件扫描元素时,AutowiredAnnotationBeanPostProcessor 和CommonAnnotationBeanPostProcessor会隐式地被包括进来。 也就是说,连个组件都会被自动检测并织入 - 所有这一切都不需要在XML中提供任何bean配置元数据。
Note
通过加入值为'false'的annotation-config属性可以禁止注册这些后置处理器。(<context:annotation-config>false</context:annotation-config>)

使用过滤器自定义扫描
默认情况下,用@Component、 @Repository、@Service或 @Controller (或本身使用了@Component注解的自定义注解) 注解的类是唯一会被检测到的候选组件。但是可以很方便地通过自定义过滤器来改变并扩展这一行为。 可以用'component-scan'的include-filter或 exclude-filter子元素来进行添加。 每个过滤器元素都要求有'type'和'expression'属性。 下面给出了四个已有的可选过滤器。

annotation
org.example.SomeAnnotation 具有指定的注解的
assignable
org.example.SomeClass 指定的类
regex
org\.example\.Default.* 正则匹配
aspectj
org.example..*Service+ aspectj方式

<beans ...>

     <context:component-scan base-package="org.example">
        <context:include-filter type="regex" expression=".*Stub.*Repository"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
     </context:component-scan>

</beans>
会忽略@Repository注解,会扫描stub的下面的库

也可以用<component-scan/>元素的use-default-filters="false" 属性来禁用默认的过滤器。这会关闭对使用了@Component、 @Repository、@Service或 @Controller的类的自动检测。

自动检测组件的命名
当一个组件在某个扫描过程中被自动检测到时,会根据那个扫描器的BeanNameGenerator 策略生成它的bean名称。默认情况下,任何包含name值的Spring“典型”注解 (@Component、@Repository、 @Service和@Controller) 会把那个名字提供给相关的bean定义。如果这个注解不包含name值或是其他检测到的组件 (比如被自定义过滤器发现的),默认bean名称生成器会返回小写开头的非限定(non-qualified)类名。 例如,如果发现了下面这两个组件,它们的名字会是'myMovieLister'和'movieFinderImpl':
@Service("myMovieLister")
public class SimpleMovieLister {
    // ...
}

@Repository
public class MovieFinderImpl implements MovieFinder {
    // ...
}

如果你不想使用默认bean命名策略,可以提供一个自定义的命名策略。首先实现 BeanNameGenerator 接口,确认包含了一个默认的无参数构造方法。然后在配置扫描器时提供一个全限定(fully-qualified)类名:
<beans ...>
              
     <context:component-scan base-package="org.example"
                             name-generator="org.example.MyNameGenerator" />

</beans>

为自动检测的组件提供一个作用域
通常受Spring管理的组件,默认或者最常用的作用域是“singleton”。然而,有时也会需要其他的作用域。 因此Spring 2.5还引入了一个新的@Scope注解。只要在注解中提供作用域的名称就行了, 比方说:
@Scope("prototype")
@Repository
public class MovieFinderImpl implements MovieFinder {
    // ...
}


@Resource

Spring 也提供了使用 JSR-250 bean 属性支持的注射方式。这是一种在 Java EE 5 与 Java 6 中普遍使用的方式(例如,在 JSF 1.2 中映射 beans 或者 JAX-WS 2.0 端点),对于Spring 托管的对象 Spring 可以以这种方式支持映射。

@Resource有一个‘name’属性,缺省时,Spring 将这个值解释为要注射的 bean 的名字。换句话说,如果遵循by-name的语法,如下例:

public class SimpleMovieLister {

    private MovieFinder movieFinder;

    @Resource(name="myMovieFinder")
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }
}
如果没有显式地给出名字,缺省的名字将继承于字段名或者 setter 方法名:如果是字段名,它将简化或者等价于字段名;如果是 setter 方法名,它将等价于 bean 属性名。下面这个例子使用名字 "movieFinder" 注射到它的 setter 方法:

public class SimpleMovieLister {

    private MovieFinder movieFinder;

    @Resource
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }
}
Note
注解提供的名字将被BeanFactory解析为 bean 名。请注意,这些名字也可能通过 JNDI 被解析(需要配置 Spring 的SimpleJndiBeanFactory)。不过,建议您依靠缺省行为与 Spring 的 JNDI 查找功能。

与@Autowired类似,@Resource可以回退为与标准 bean 类型匹配(例如,使用原始类型匹配取代特殊命名 bean)来解决著名的"resolvable dependencies":BeanFactory 接口,ApplicationContext 接口,ResourceLoader 接口,ApplicationEventPublisher 接口以及 MessageSource 接口。请注意:这只有适用于未指定命名的@Resource!

下面的例子有一个customerPreferenceDao字段,首先要查找一个名叫 “customerPreferenceDao” 的 bean,然后回退为一个原始类型以匹配类型CustomerPreferenceDao。"context" 字段将基于已知解决的依赖类型ApplicationContext而被注入。

public class MovieRecommender {

    @Resource
    private CustomerPreferenceDao customerPreferenceDao;

    @Resource
    private ApplicationContext context;

    public MovieRecommender() {
    }

    // ...
}

@PostConstruct 与 @PreDestroy
CommonAnnotationBeanPostProcessor 不只是能识别@Resource注解,而且也能识别 JSR-250 lifecycle注解。

CommonAnnotationBeanPostProcessor已经在 Spring 的ApplicationContext中注册,当一个方法带有这些注解之一时,将被在其生命周期与 Spring 生命周期接口的方法或者显式声明回调方法同一刻上调用。下面的例子里,缓存将预置于初始化与销毁阶段。

public class CachingMovieLister {

    @PostConstruct
    public void populateMovieCache() {
        // populates the movie cache upon initialization...
    }
   
    @PreDestroy
    public void clearMovieCache() {
        // clears the movie cache upon destruction...
    }
}
  • 大小: 7.1 KB
分享到:
评论

相关推荐

    通过@Autowired注解注入bean的顺序,以及@bean注入.rar

    `@Bean`相当于一个简化版的工厂bean,因为它直接在配置类中定义了bean的创建逻辑。而传统的`factoryBean`则是通过实现`FactoryBean`接口来创建bean,这提供了更大的灵活性,但使用起来相对复杂。 结合`@Service`...

    spring3零配置注解实现Bean定义

    描述部分提到的“零配置注解实现Bean定义”是在Spring 3中为了简化Spring Bean的配置而引入的一种新的配置方式。它让开发者可以通过注解的方式定义和注入Bean,而无需在XML配置文件中显式地进行配置。这是Spring框架...

    JavaEE 使用注解配置Bean的一个示例

    在JavaEE开发中,注解(Annotation)已经成为了一种强大的工具,它允许开发者在代码中嵌入元数据,简化了传统的XML配置方式。本示例将深入探讨如何使用注解来配置Bean,使得应用程序的配置更加简洁、直观。 首先,...

    自定义注解得使用,模拟spring通过注解方式创建bean实例

    Spring的注解功能极大地简化了配置,使得开发者可以更专注于业务逻辑。本篇将深入探讨如何自定义注解并模拟Spring通过注解方式创建bean实例。 首先,了解注解(Annotation)在Java中的角色。注解是一种元数据,它...

    跟我学Spring3(12.3)零配置之注解实现Bean定

    传统的Spring配置方式是通过XML文件来定义Bean及其依赖关系,而现在,我们可以使用注解来简化这个过程。 1. `@Component`注解:这是Spring提供的基础注解,用于标记一个类为Spring管理的Bean。你可以使用它的派生...

    spring3零配置注解实现Bean定义(包括JSR-250、JSR-330)

    随着技术的发展与需求的变化,Spring为了简化配置过程,引入了注解的方式进行Bean的定义。这不仅减少了XML配置的数量,同时也提供了更加灵活和简洁的开发方式。本文将详细介绍如何利用Spring3提供的注解特性实现Bean...

    IOC之基于注解的配置bean(下)

    总结,基于注解的配置bean是Spring框架中一种强大的特性,它简化了bean的声明和管理,增强了代码的可读性和可维护性。通过上述讲解,我们可以了解到如何在Spring中使用注解进行bean的定义、依赖注入、AOP等方面的...

    Spring的Bean配置

    综上所述,Spring的Bean配置是其核心机制之一,它涉及到IoC和DI原则的应用,以及多种配置方式的选择,如XML、注解和Java配置。理解并熟练掌握这些概念和实践,对于有效管理Java应用的复杂性至关重要。在实际项目中,...

    spring bean XML配置入门

    在实际开发中,我们可以使用Eclipse的Spring插件来简化Bean配置的创建和管理,同时结合Maven来构建和运行Spring应用。 通过以上内容,你应该对Spring框架中的Bean XML配置有了初步的理解。掌握这些知识点后,你将...

    使用注解方式配置的spring_mvc_3.0

    Spring 2.5版本开始,为了迎合这一趋势,引入了全面的基于注解的Bean配置和装配功能,这标志着开发者可以不再依赖于XML配置文件,而是利用Java代码内的注解来完成相同的任务。本文将深入探讨Spring 2.5基于注解的IoC...

    spring注解开发--Bean加载控制.zip

    `@Profile`注解允许你在不同环境(如开发、测试、生产)中使用不同的bean配置。你可以指定bean应该在哪些配置环境下激活。 8. **@PostConstruct和@PreDestroy** 这两个注解标记方法分别在bean初始化后和销毁前...

    Spring--2.Spring 中的 Bean 配置-4

    在Spring框架中,Bean配置是核心概念之一,它关乎到对象的创建...XML适合大型项目,注解简化了配置,而Java配置则提供了最大的灵活性和可编程性。理解并熟练掌握这些配置方式,对于提升Spring框架的使用效率至关重要。

    SpringMVC全注解配置

    2. **组件扫描**:通过在Spring配置类上使用`@ComponentScan`注解,Spring可以自动发现并注册带有指定注解的bean。这减少了对XML配置文件的需求。 3. **DispatcherServlet配置**:在Servlet 3.0及以上版本,我们...

    线程中获取spring 注解bean

    这些注解使得我们可以在不编写XML配置的情况下,声明和管理bean的生命周期及依赖关系。 当需要在线程中获取Spring注解的bean时,有几种常见的方法: 1. **ThreadLocal**:Spring提供了一种名为`...

    spring famework 基于注解配置示例

    `@Configuration`注解告诉Spring这是一个配置类,而`@ComponentScan`则指定Spring要扫描哪些包来寻找带有注解的bean。 此外,Spring还提供了其他的注解来简化配置,比如: - `@Value`:用于注入属性值,可以从属性...

    ssm纯注解配置例子.rar

    首先,Spring框架提供了丰富的注解来简化配置,如`@Component`、`@Service`、`@Repository`和`@Controller`,它们分别用于标记普通的Bean、服务层、数据访问层和控制器层的类。这些注解配合`@Autowired`可以自动装配...

    丛林探险之Spring自定义注解加载Bean

    这个机制在实际开发中有着广泛的应用,比如在微服务框架如Dubbo中,通过自定义注解可以方便地将服务提供者和服务消费者自动注册到Spring容器,简化配置,提高代码可读性和可维护性。通过理解和掌握这一机制,开发者...

    注解配置 javabean hibernate 关联关系

    在Hibernate框架中,注解配置被广泛用于简化对象关系映射(ORM)的配置,使得Java Bean可以直接与数据库表进行关联。本篇文章将深入探讨如何使用注解配置Java Bean并建立Hibernate的关联关系。 首先,我们需要理解...

    spring配置文件----注解配置

    注解配置是Spring框架的一种简化配置手段,它允许开发者通过在类或方法上添加特定的注解,替代传统的XML配置文件,使得代码更加简洁且易于维护。 首先,我们需要理解Spring配置文件的作用。在早期的Spring版本中,...

    Spring IOC之基于注解的容器配置.docx

    Spring IOC,全称为Inversion of Control,即控制反转,是Spring框架的...注解配置简化了代码,提高了开发效率,而XML配置则提供了更大的灵活性和控制权。理解并熟练掌握这两种配置方式,对于Spring开发来说至关重要。

Global site tag (gtag.js) - Google Analytics