当前位置:网站首页>@component使用案例
@component使用案例
2022-06-30 14:10:00 【全栈程序员站长】
@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 {
@Autowired
private UserDaoImpl userDaoImpl;
@Test
public void test() {
//启动spring容器,因为是测试类,所以要使用applicationContext.如果是tomcat启动就不用。
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext5.xml");
userDaoImpl=(UserDaoImpl) ac.getBean("impl");
userDaoImpl.aa();
}
}发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/106264.html原文链接:https://javaforall.cn
边栏推荐
- Introduction to the renewal of substrate source code: the pallet alliance is incorporated into the main line,
- Multi terminal collaboration of Huawei accounts to create a better internet life
- 知识传播不能取代专业学习!
- 第四批入围企业公示——年度TOP100智能网联供应商评选
- 科普达人丨漫画图解什么是eRDMA?
- Small exercise of process and signal
- Cost forecast of PMP (BAC, EAC, etc)
- 随着产业互联网的发展,有关互联网的落地和应用也就变得宽阔了起来
- [the path of system analyst] Chapter 5 Software Engineering (Agile Development)
- JMeter transaction controller
猜你喜欢

科普达人丨漫画图解什么是eRDMA?
![[scientific research data processing] [practice] frequency analysis chart of category variables, distribution chart of numerical variables and normality test (including lognormal)](/img/5a/eaa845f4332f0b8ee8b6409d6a79e8.png)
[scientific research data processing] [practice] frequency analysis chart of category variables, distribution chart of numerical variables and normality test (including lognormal)

【科学文献计量】外文文献及中文文献关键词的挖掘与可视化

QQ 居然被盗了?原因在这......

Jetpack compose for perfect screen fit

Solve the error in my QT_ thread_ global_ End(): 3 threads didn't exit

What is erdma as illustrated by Coptic cartoon?

Geoffreyhinton: my 50 years of in-depth study and Research on mental skills

重磅:国产IDE发布,由阿里研发,完全开源!

IM即时通讯应用开发中无法解决的“顽疾”
随机推荐
Unity 频繁切换分支 结果模型出现莫名其妙的错误
数据库表为什么写不进数据了
深入理解.Net中的线程同步之构造模式(二)内核模式2.内核模式构造物Semaphone
Lucky hash quiz system development (source code deployment) fun investment hash game play development (case requirements)
第四批入围企业公示——年度TOP100智能网联供应商评选
exlipse同时操作多行。比如同时在多行同列输入相同的文字
数字化转型道阻且长,如何迈好关键的第一步
Rpm2rpm packaging steps
Unity animator parameter
可觀測,才可靠:雲上自動化運維CloudOps系列沙龍 第一彈
How to execute a query SQL
损失函数:DIOU loss手写实现
Getting started with shell Basics
MFQE 2.0: A New Approach for Multi-FrameQuality Enhancement on Compressed Video
SQL programming problem, test case failed
Google Earth engine (GEE) -- converts string to number and applies it to time search (ee.date.fromymd)
点击table的td单元格出现dialog弹窗,获取值后将值放回td单元格
Introduction to reverse commissioning - VA and RVA conversion in PE 04/07
Directory related commands
Comprehensively analyze the basic features and summary of free and paid SSH tools