spring利用扫描方式对bean的处理(对任何版本如何获取xml配置信息的处理)

利用扫描的方式将组件注入容器,就也可以不用操作bean来实例化对象了。

下面我做一个例子

我用的spring3.2.2版本的

首先写一个spring.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.xsd

         http://www.springframework.org/schema/context

         http://www.springframework.org/schema/context/spring-context.xsd">

 

<context:component-scan base-package="com.fish"/>//这里写包名。意思说只要在这个包里的bean都被我实例化了。

</beans>

 

其实这篇代码不难,但是对于新手学习还是有点难度的。因为spring的版本不一样。所以配置信息的内容会改变。如何获取自己版本对应的配置信息呢?首先明白所有框架你在从官网下载下来一般都有这3部分,第一lib文件存放所有jar包,第二个就是doc就是说明书,第三个就是例子。

你只要知道自己版本的说明书现在官网的都以网页形式给出。比如说我现在学习的是IOC

bean处理。那么我到找关于bean处理的网页,在浏览器里面查找,<xmlns:context一般就能找到了你要的模板例子了。这个复制粘贴到你的xml那绝对是100%正确的。什么版本都不用怕。

 

接着我写一个person.java

package com.fish;

 

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.stereotype.Service;

 

@Service   //类前面加个注解。而这文件本来就在com.fish包下,这样spring容器就知道你要实例化了

public class Person {

public String getName(){

       return name;

}

 

public voidsetName(String name) {

       this.name = name;

}

 

public void show(){

       System.out.println("aa");

}

}

下面我在写一个测试类吧

package com.fish;

 

importorg.springframework.context.ApplicationContext;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

 

public class Test {

      

public static voidmain(String[] args) {

       ApplicationContext context =

                  newClassPathXmlApplicationContext("spring.xml");//spring激活,

                     Person person=(Person)context.getBean("person");//本来是靠bean来建立对象的,现在XML文件早就没有了。所以这个bean的名字。就是你要初始化的类名的小写开头。

                     person.show();//我们可以看到输出aa

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <description>Spring公共配置文件</description> <!-- mes 的數據庫 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/> <property name="jdbcUrl" value="jdbc:oracle:thin:@10.142.252.132:1521:mestest"/> <property name="maxPoolSize" value="10"></property> <property name="maxIdleTime" value="1800"></property> <property name="minPoolSize" value="1"></property> <property name="initialPoolSize" value="1"></property> <property name="properties"> <ref bean="mesDatasourcePropertiesFactory" /> </property> </bean> <!-- c3p0数据源的一个专有属性,只可以存放密码和用户名 --> <bean id="mesDatasourcePropertiesFactory" class="com.ccc.db.impl.DatasourcePropertiesFactory" factory-method="getProperties"> <!-- userName--> <constructor-arg type="java.lang.String"> <value>jxg/Qr4VbxU=</value> </constructor-arg> <!-- password --> <constructor-arg type="java.lang.String"> <value>jxg/Qr4VbxU=</value> </constructor-arg> <!-- 生产环境模式 ,才特殊处理加密密码--> <constructor-arg type="java.lang.String"> <value>true</value> </constructor-arg> </bean> <!-- ptc windchill的數據庫 --> <bean id="dataSourcePdm" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/> <property name="jdbcUrl" value="jdbc:oracle:thin:@10.142.252.132:1521:mesdev"/> <property name="maxPoolSize" value="10"></property> <property name="maxIdleTime" value="1800"></property> <property name="minPoolSize" value="1"></property> <property name="initialPoolSize" value="1"></property> <property name="properties"> <ref bean="ptcDatasourcePropertiesFactory" /> </property> </bean> <!-- c3p0数据源的一个专有属性,只可以存放密码和用户名 --> <bean id="ptcDatasourcePropertiesFactory" class="com.ccc.db.impl.DatasourcePropertiesFactory" factory-method="getProperties"> <!-- userName--> <constructor-arg type="java.lang.String"> <value>WgDH/SDIJfs=</value> </constructor-arg> <!-- password --> <constructor-arg type="java.lang.String"> <value>WgDH/SDIJfs=</value> </constructor-arg> <!-- 生产环境模式 ,才特殊处理加密密码--> <constructor-arg type="java.lang.String"> <value>true</value> </constructor-arg> </bean> <!-- mes數據源代理 --> <bean id="dataSourceProxy" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy" p:targetDataSource-ref="dataSource"/> <!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能--> <context:component-scan base-package="com.ccc"/> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="0" /> <!-- 配置事务管理器 針對MES數據庫--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager " p:dataSource-ref="dataSourceProxy"/> <!-- 配置事务的传播特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 那些类的哪些方法参与事务 --> <aop:config> <aop:pointcut id="allManagerMethod" expression="execution(* com.ccc..*.*(..))"/> <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/> </aop:config> <!-- 配置事务管理器,這個事務性是爭對pdm數據庫的 --> <bean id="transactionManagerPdm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager " p:dataSource-ref="dataSourcePdm"/> <!-- 配置事务的传播特性 --> <tx:advice id="txAdvicePdm" transaction-manager="transactionManagerPdm"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 那些类的哪些方法参与事务 --> <aop:config> <aop:pointcut id="allManagerMethodPdm" expression="execution(* com.ccc.pdm..*.*(..))"/> <aop:advisor pointcut-ref="allManagerMethodPdm" advice-ref="txAdvicePdm"/> </aop:config> <!-- ibatis插件 --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean" p:dataSource-ref="dataSourceProxy"> <property name="configLocation"> <value>classpath:SqlMapConfig.xml</value> </property> </bean> <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> <property name="sqlMapClient"> <ref bean="sqlMapClient" /> </property> </bean> <!-- 配置要拦截的url,防止2次提交或做其他數據統計用 <bean id="doubleSubmitInterceptor" class="com.ccc.filter.DoubleSubmitInterceptor"> <property name="mappingURL" value=".html" /> <property name="viewURL" value=".html" /> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="0"> <property name="interceptors"> <list> <ref bean="doubleSubmitInterceptor"/> </list> </property> </bean> --> <!-- JDBC template注入及事務配置 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"><ref bean="dataSourceProxy"/></property> </bean> </beans>
1 Spring基本特征 6 2 Spring的组成 6 2.1 Spring的jar包 6 2.2 Spring配置文件 7 2.3 Spring API 8 3 Spring基本功能详解 8 3.1 SpringIOC 8 3.2别名Alias 11 别名拓展: 11 3.3 Spring容器内部对象的创建 12 Spring容器内部对象创建拓展: 12 3.3.1使用类构造器实例化(默认无参数) 14 3.3.2使用静态工厂方法实例化(简单工厂模式) 14 3.3.3初始化(创建)bean时机 15 Lazy-init初始化bean的时机拓展: 15 3.4 Bean的作用域 16 Scope单例多例作用域拓展: 16 3.4.1 singleton(默认值) 16 3.4.2 prototype 17 3.4.3 Request 17 3.4.4 Session 18 3.4.5 Global session 18 3.4.6 指定Bean的初始化方法和销毁方法 18 Bean的初始化和销毁拓展: 18 Spring的IOC总结: 20 3.5 依赖注入(DI) 20 3.5.1 使用构造器注入 20 3.5.2 使用属性setting方法进行注入 21 3.5.3 装配list集合 22 3.5.4 装配set集合 22 3.5.5 装配map 22 3.5.6 装配Properties 23 3.6 注解注入 23 注解注入拓展: 23 3.6.1 @Autowired 26 3.6.2 @Qualifier 27 3.6.3 @Resource 27 3.6.4 @PostConstruct 28 3.6.5 @PreDestroy 28 注解注入拓展: 28 3.7扫描注入 30 注解扫描拓展: 32 Mvc用注解写: 34 Spring容器IOC和di的整个启动过程: 38 3.8 spring中的继承 38 拓展spring为类中的属性赋值: 40 小结: 47 面向接口编程: 47 4 面向切面编程 52 4.1 代理模式 52 代理模式拓展: 52 4.1.1 JDK动态代理 58 JDK动态代理拓展: 59 4.1.2 CGLIB做代理 66 CGLIB动态代理拓展: 68 4.1.3 Spring的动态代理 71 4.2 AOP编程 71 4.2.1概念: 71 SpringAOP概念拓展: 73 之前实现了目标方法的动态调用,现在来实现切面的动态调用。 74 4.2.2 AOP实现的两种模式 78 4.2.2.1 xml形式 78 XML形式拓展: 81 异常通知处理例子: 91 不用spring异常通知,另一种处理异常 96 4.2.2.2Aop注解形式(了解) 99 注解注入拓展: 103 5 Spring数据库 106 5.1 Spring+JDBC 106 5.1.1 Jdbc编程特点 106 5.1.2引入DataSource 106 5.1.3 核心类JdbcTemplate 106 5.1.4 使用JdbcTemplate 106 5.1.5 继承JdbcDaoSupport 107 5.1.6 使用properties文件 107 5.1.7 RowMapper的使用 107 拓展: 108 DataSource注入的三种方式: 108 5.1.8声明式事务管理 116 5.1.8.1Spring的事务管理器 117 5.1.8.2Spring事务的传播属性 117 5.1.8.3Spring事务的隔离级别 117 拓展: 118 5.1.8.4以XML配置的 形式 119 拓展: 120 5.1.8.5以注解方式配置 125 拓展: 127 5.1.9使用CGLIB以XML形式配置事务 130 5.2 Spring+Hibernate 131 5.2.1 HibernateTemplate模板 131 5.2.2 声明式事务 131 配置XML文件 131 拓展: 132 注解形式: 137 拓展: 138 6 Struts2+spring+hibernate 141 6.1 需要添加的jar包 141 6.2 Spring融合web服务器 141 6.3 struts.xml文件 143 6.4 OpenInSessionView 143 拓展: 144 实例: 146
### 回答1: Spring通过扫描指定的包路径来查找bean定义,并将它们注册到应用程序上下文中。Spring使用`@ComponentScan`注解来指定需要扫描的包路径。当使用`@ComponentScan`注解时,Spring扫描所有被`@Component`、`@Service`、`@Repository`和`@Controller`注解的类,并将它们注册为bean。 例如,以下代码演示了如何在Spring中使用`@ComponentScan`注解扫描名为`com.example`的包下的所有bean定义: ```java @Configuration @ComponentScan("com.example") public class AppConfig { // ... } ``` 除了`@ComponentScan`注解外,还可以使用`@Bean`注解手动注册bean,或者使用XML配置文件来定义bean。 ### 回答2: Spring框架通过Bean扫描机制可以自动检测和加载应用程序中的Bean。 Spring会在应用程序的类路径上扫描指定的包或类,寻找被Spring管理的Bean。这个过程通常在应用程序启动时发生,Spring会搜索并加载符合条件的类,并将其注册为Bean定义。Spring框架提供了几种方式配置Bean的扫描路径。 首先,可以通过使用@ComponentScan注解来开启Bean的自动扫描。在配置类上加上@ComponentScan注解,并指定需要扫描的包路径,Spring框架将会递归地扫描这些包,查找被@Component、@Service、@Repository、@Controller等注解修饰的类,并将其注册为Bean。 其次,可以使用XML配置文件来进行扫描Bean。在配置文件中使用<context:component-scan>元素,并设置base-package属性为需要扫描的包路径,Spring框架会自动扫描该包下所有被标记的类,并加载注册为Bean。 除此之外,还可以使用基于Java的配置方式,通过编写配置类来定义Bean的扫描路径。在配置类上使用@Configuration和@ComponentScan注解,指定需要扫描的包路径,Spring框架会自动搜索并加载满足条件的类。 总之,无论是通过注解还是XML配置文件,Spring框架可以通过不同的方式扫描Bean,从而实现自动加载和注册。这样可以避免手动配置每个Bean的信息,提高开发效率,使应用程序更加灵活和可扩展。 ### 回答3: Spring框架通过使用不同的机制来扫描和注册bean。 首先,Spring可以通过基于XML配置文件来扫描bean。在XML配置文件中,我们可以使用`<bean>`元素来定义一个bean,并通过`id`或`name`属性来标识它。通过在`<bean>`元素中配置适当的信息,如类的全限定名、构造函数参数和属性值,Spring可以实例化和管理这些bean。然后,通过在XML配置文件中使用`<context:component-scan>`元素,可以使Spring自动扫描指定路径下的所有带有特定注解的类,并将它们注册为bean。 其次,Spring还可以通过基于注解的方式扫描bean。通过在类上使用`@Component`注解或其派生注解(如`@Controller`、`@Service`、`@Repository`等),我们可以指示Spring将该类注册为一个bean。同时,通过使用`@Autowired`注解,我们可以将其他依赖的bean自动注入到当前bean中,实现依赖注入的功能。在启动Spring应用程序时,Spring会自动扫描指定路径下的所有类,并将被注解标记的类注册为bean。 最后,Spring还支持自定义扫描和注册bean的机制。我们可以实现`BeanDefinitionRegistryPostProcessor`接口来编写自定义的bean扫描器,通过编程方式将类注册为bean。我们可以根据自己的需求定义扫描路径、过滤条件等,实现更加灵活的bean扫描和注册逻辑。 总结起来,Spring可以通过XML配置文件、基于注解的方式以及自定义扫描器等多种机制来扫描和注册bean,从而实现对应用程序中的组件的管理和控制。这些机制使开发人员能够以更加便捷和灵活的方式管理和使用bean。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值