小编Khu*_*ush的帖子

在Spring中运行时注册bean(原型)

只需要社区评估的东西.以下是一段代码,它是一个创建特定类型实例的简单工厂.该方法将在上下文中将bean注册为原型并返回实例.这是我第一次在运行时配置bean.你能评价并提供反馈意见吗?先感谢您.

package au.com.flexcontacts.flexoperations;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.AbstractApplicationContext;

import au.com.flexcontacts.exceptions.SyncClassCreactionError;

/**
 * @author khushroo.mistry
 * Class purpose: Simple Factory to create an 
 * instance of SynchroniseContactsService and register it in the Spring IoC.
 */
public final class FLEXSyncFactory implements ApplicationContextAware {

    private static AbstractApplicationContext context;


    /**
     * @param username
     * @param password
     * @param syncType
     * @return the correct service class
     * @throws SyncClassCreactionError
     * The method registers the …
Run Code Online (Sandbox Code Playgroud)

java spring dependency-injection spring-mvc

21
推荐指数
2
解决办法
5万
查看次数

Spring-mvc控制器和异常处理

想问一个关于spring-mvc控制器的最佳实践问题.请查看以下代码:

    @Autowired
    SomeService service;

    @RequestMapping (...)
    public @ResponseBody Response createSomething () {

       try {

            serviceResponse = service.doSomething();

            //create a success response and return

       }
       catch (SomeServiceException e) {
             //create an error response and return 
       }

}
Run Code Online (Sandbox Code Playgroud)

错误处理是否在控制器级别正常进行?或者服务类是否应该抛出如上所示的异常.请查看并告知我们.

java spring spring-mvc

7
推荐指数
1
解决办法
1万
查看次数

使用Spring AOP引入新功能

我正在使用Spring In Action(第3版)一书学习Spring.我在书中遇到了一个不适合我的例子.以下是详细信息:

我试图使用Spring AOP在bean中引入新功能:

这是类结构:

  1. 乐器演奏者实现了演奏者
  2. GraciousContestant实现了参赛者

执行者界面

public interface Performer {
    void perform();
}
Run Code Online (Sandbox Code Playgroud)

班级乐器演奏家

package chapter4;

public class Instrmentalist implements Performer {
    public void perform() {
        System.out.println("Sining hey hey, hey");
    }
}
Run Code Online (Sandbox Code Playgroud)

参赛者界面

package chapter4;

public interface Contestant {
    void receiveAward();
}
Run Code Online (Sandbox Code Playgroud)

GraciousContestant Class

package chapter4;

public class GraciousContestant implements Contestant {
    public void receiveAward() {
        System.out.println("Won a car worth 4K!");
    }
}
Run Code Online (Sandbox Code Playgroud)

我的Spring配置文件名为"spring-idol2.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: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-3.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/context …
Run Code Online (Sandbox Code Playgroud)

java aop spring

4
推荐指数
1
解决办法
4133
查看次数

标签 统计

java ×3

spring ×3

spring-mvc ×2

aop ×1

dependency-injection ×1