Spring入门之bean的配置

本文介绍了Spring框架中Bean的三种配置方式:基于XML配置、基于注解定义和基于Java类提供Bean定义信息。详细阐述了每种配置方式的特点及使用场景,并提供了具体的配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

bean的配置有三种方式:
1.基于XML配置bean

2.基于注解定义bean

3.基于java类提供bean类的定义信息
基于XML配置bean
对于基于XML的配置,Spring 2.0以后使用Schema的格式,使得不同类型的配置拥有了自己的命名空间,使配置文件更具扩展性。
bean类的基本配置:

这里写图片描述

配置实例
<bean id="hello" class="org.spring.HelloSpring">
    <property name="message" value="chengxi" />
</bean>
基于注解定义bean类
常用的注解:
@Component大致可以分为如下几个注解:
      a.@controller控制器(注入服务),用于标注控制层组件(如struts中的action) 
      b.@service服务(注入dao),用于标注业务层组件
      c.@repository dao(实现dao访问),用于标注数据访问组件,即DAO组件
      d.@component (把普通pojo实例化到spring容器中),泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注

@autowired:用于自动注入bean:如
@Autowired
private UserDao userDao;
通过使用注解将普通的java类注解之后,还需要在xml配置文件中指定扫描这些类,用于自动将这些注解类注解成bean类,通常将这些类放置在同一个主包下面的不同的包里面,用于集中扫描管理
<!--自动扫描com.spring包下的所有注解类并注册成bean类-->
<context:component-scan base-package="com.spring" />

<!--如果指定扫描一些包,可以使用如下语法-->
<context:component-scan base-package="com.spring">
    <context:include-filter type="regex" expression=".*DaoImpl" />
    <context:include-filter type="regex" expression=".*ServiceImp" />
    <!--<context:excluse-filter type="regex" expression=".*Controller" />-->
</context:component-scan>
基于注解的bean注册使用实例
//业务逻辑层Service bean
@Service("testService")
public class TestService{

    public void show(){
        System.out.println("业务逻辑层");
    }
}

//数据访问层Repository bean
@Repository("testDao")
public class TestDao{
    public void show(){
        System.out.println("数据访问层");
    }
}

//控制层Controller bean
@Controller("testController")
public class TestController{
    public void show(){
        System.out.println("控制层");
    }
}

//bean.xml配置文件
<?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-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

        <context:component-scan base-package="org.spring.annotation" />

</beans>

//测试主程序
public class testMain{
    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"_

        TestDao testDao = context.getBean("testDao",TestDao.class);
        TestService testService = context.getBean("testSercvice",TestService.class);
        TestController testController = context.getBean("testController",TestController.class);

        testDao.show();
        testService.show();
        testController.show();
基于java类提供bean定义
在普通的POJO类中只要标注@Configuration注解,就可以为spring容器提供Bean定义的信息了,每个标注了@Bean的类方法都相当于提供了一个Bean的定义信息。
实现代码:
package com.baobaotao.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConf {

    @Bean
    public UserDao userDao(){
       return new UserDao();    
    }

    @Bean
    public LogDao logDao(){
        return new LogDao();
    }

    @Bean
    public LogonService logonService(){
        LogonService logonService = new LogonService();
        logonService.setLogDao(logDao());
        logonService.setUserDao(userDao());
        return logonService;
    }
}

//xml配置
<beans ...>
    <context:component-scan base-package="org.spring" />
</beans>

//测试主程序
public class testMain{
    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Person person = context.getBean("person",Person.class);
        person.setName("hello");
        System.out.println(person.getName());
    }
}
以上的配置方式可以等价于如下的xml配置
<bean id="userDao" class="org.spring.UserDao" />
<bean id="logDao" class="org.spring.LogDao" />
<bean id="logoonService" class="org.spring.LogonService" p:userDao-ref="userDao" p:logDao-ref="logDao" />
@Configuation和@Bean两个注解的用法
用@Configuration注解该类,等价于在XML中配置beans;用@Bean标注方法等价于在XML中配置bean。
AnnotationConfigApplicationContext 搭配上 @Configuration 和 @Bean 注解,自此,XML 配置方式不再是 Spring IoC 容器的唯一配置方式。两者在一定范围内存在着竞争的关系,但是它们在大多数情况下还是相互协作的关系,两者的结合使得 Spring IoC 容器的配置更简单,更强大。 之前,我们将配置信息集中写在 XML 中,如今使用注解,配置信息的载体由 XML 文件转移到了 Java 类中。我们通常将用于存放配置信息的类的类名以 “Config” 结尾,比如 AppDaoConfig.java、AppServiceConfig.java 等等。我们需要在用于指定配置信息的类上加上 @Configuration 注解,以明确指出该类是 Bean 配置的信息源。并且 Spring 对标注 Configuration 的类有如下要求:
1.配置类不能是 final 的;

2.配置类不能是本地化的,亦即不能将配置类定义在其他类的方法内部;

3.配置类必须有一个无参构造函数。



文档参考
bean与spring容器的关系
解释@Component @Controller @Service @Repository
Spring基于 Annotation 的简单介绍
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值