@component使用案例
来源:互联网 发布:mac usb是2.0还是3.0 编辑:程序博客网 时间:2024/06/10 23:20
@component的作用是将一个java类加入到spring容器中管理,省去了<bean id="" class="">这句配置,取而代之的是<context component-scan base-package="包名" />,负责扫描改包下的所有包名。
案例:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd "> <context:component-scan base-package="com.aa"></context:component-scan> </beans>
package com.aa;import org.springframework.stereotype.Component;import com.introduce.Model.User2;@Component("impl")public class UserDaoImpl {public User2 getUser2() {return null;}public User2 getUser2(String name) {User2 user2=new User2();user2.setAge(22);System.out.println(name+"s1");return user2;}public void aa(){System.out.println("bbbbb");}}
import org.junit.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.aa.UserDaoImpl;public class Test4 {@Autowiredprivate UserDaoImpl userDaoImpl;@Testpublic void test() {//启动spring容器,因为是测试类,所以要使用applicationContext.如果是tomcat启动就不用。ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext5.xml");userDaoImpl=(UserDaoImpl) ac.getBean("impl");userDaoImpl.aa();}}
文件布局:
阅读全文
0 0
- @component使用案例
- Swing component使用方式
- Intent中Component使用
- Intent使用Component
- Intent中Component使用
- Intent中Component使用
- hibernate的Component使用
- 组件使用component
- vue component 组件使用
- @Component注解的使用
- Vue.component使用注意事项
- <context:component-scan> 的使用
- Salesforce chatter:userPhotoUpload Component 使用
- Symfony Asset Component 使用简述
- <context:component-scan>使用详情
- <context:component-scan>使用详解
- Spring:使用@Component进行注解
- @Component
- 2017年第3届上海国际零售业设计与设备展会刊(参展商名录)
- HBase之缓存
- 百(千)万级excel导出(流式)
- Check failed: registry.count(type) == 1 (0 vs. 1) Unknown layer type
- 关于迭代集合同时删除元素报错的问题java.util.ConcurrentModificationException
- @component使用案例
- 重启Tomcat
- [转]软件开发中的deadline该怎么定?
- 《大话设计模式》java实现之装饰器模式
- android 判断是否在debug模式
- 前端基础-01-html标签
- linux 哲学家进餐问题
- Apache配置Let’s Encrypt免费SSL证书
- 习题5.4