最小化改动将已有的maven-java项目改造/配置为spring项目,并成功启动访问spring容器管理的bean对象方法

作者: admin 分类: 环境搭建 发布时间: 2019-07-04 19:26  阅读: 106 views

早期有做过几个简单的java项目,每个对象需要new来new去的,比较麻烦。所以,直接改造为spring项目,bean对象交由spring管理。减少一些程序猿的搬砖过程。

1.创建一个简单的maven – java项目(非web项目,原有java项目也可以)
2.添加spring依赖包

<properties>
	<spring.version>4.3.8.RELEASE</spring.version>
</properties>
<dependencies>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.11</version>
		<scope>test</scope>
	</dependency>
	<!-- spring-core -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-core</artifactId>
		<version>${spring.version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-aop</artifactId>
		<version>${spring.version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-beans</artifactId>
		<version>${spring.version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
		<version>${spring.version}</version>
	</dependency>
<dependencies>

3.新建接口与实现类(或者原类上改下)

package com.chl.service;

public interface BasicService {

	/**
	 * 问候语方法
	 * @param greeting
	 * @return
	 */
	String sayHello(String username,String greeting);
}
package com.chl.service.impl;

import org.springframework.stereotype.Service;

import com.chl.service.BasicService;

@Service("basicService")
public class BasicServiceImpl implements BasicService {

	@Override
	public String sayHello(String username,String greeting) {
		return "来自"+username+"的问候:" + greeting;
	}

}

4.增加resources资源文件夹,并创建spring.xml文件

目录为  src/main/resources/spring/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-3.0.xsd">

	<!-- 注入bean对象,可以扫描注解,简化配置 -->
	<!-- <bean id="basicService" class="com.chl.service.impl.BasicServiceImpl"></bean> -->
	
	<!-- 扫描包自动注入 -->
	<context:component-scan base-package="com.chl.service.impl.**"></context:component-scan>
	
	<!-- 导入相关资源包 -->
	<import resource="spring-dubbo-provider.xml"/>

</beans>

5. 增加启动类

package com.chl;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.chl.service.BasicService;

/**
 * Hello world!
 *
 */
public class Run 
{
    public static void main( String[] args )
    {
    	
    	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:spring/spring.xml");
    	BasicService bs = (BasicService)context.getBean("basicService");
    	context.start();
    	System.out.println(bs.sayHello("ysm", "huge"));
    }
}

6.如果发现以下异常,请检查注解、bean名称等

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'basicServices' available
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1207)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081)
	at com.chl.Run.main(Run.java:15)

7. 如果一切正常的话,可正确输出方法内容

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
来自ysm的问候:huge

 

 


   原创文章,转载请标明本文链接: 最小化改动将已有的maven-java项目改造/配置为spring项目,并成功启动访问spring容器管理的bean对象方法

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注

更多阅读