1、导入jar包
<!--spring5.x的jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
2、配置applicationContext.xml
id为每一个bean的唯一标识,可写可不写。
<bean>标签中可以给需要创建的类的构造方法初始化值
<bean class="redis.clients.jedis.JedisPool">
<constructor-arg name="host" value="192.168.1.102"/>
<constructor-arg name="port" value="6379"/>
</bean>
<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" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--默认方法创建对象,单例方法。对象初始化的时候调用方法--> <bean id="studentService" init-method="init" class="com.shankai.text"/> <!--bean后处理器--> <!--<bean id="post" class="com.shankai.proxy.PodtProxy"/>--> </beans>
3、创建测试类,无需new对象
ApplicationContext classPath = new ClassPathXmlApplicationContext("applicationContext.xml");
UserServiceImpl bean = (UserServiceImpl) classPath.getBean("UserService");
bean.addUser();
此标签使autoware生效,包含在<context:component-scan中
<context:annotation-config></context:annotation-config>
原文:https://www.cnblogs.com/shank/p/10299676.html