@Autowired通过构造函数按类型查找bean.如何使用自动装配的注释将bean按名称注入构造函数?我有2个相同类型的bean但我需要根据bean名称将它注入另一个相同类的构造函数.我该怎么做?
XML:
<bean id="A" class="com.Check"/>
<bean id="B" class="com.Check"/>
Run Code Online (Sandbox Code Playgroud)
Java的:
Class C {
private Check check;
@Autowired
public C(Check check){
this.check = check
}
}
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我得到一个异常告诉我,我有2个相同类型的bean检查,但它要求只有一个这种类型的bean.如何通过构造函数注入将bean id="B"注入此类C?
在我的applicationContext.xml我已经提到autowire="byType".我byName只需要在这个特定的类休息时自动装配它只需要通过类型自动装配
我想知道是否可以@Resource在构造函数上使用注释.
我的用例是我想连接一个名为的最后一个字段bar.
public class Foo implements FooBar {
private final Bar bar;
@javax.annotation.Resource(name="myname")
public Foo(Bar bar) {
this.bar = bar;
}
}
Run Code Online (Sandbox Code Playgroud)
我收到一条消息,指出@Resource此位置不允许这样做.有没有其他方法可以连接最后一个字段?
我有一个使用Spring进行依赖注入的系统.我使用基于注释的自动装配.通过组件扫描发现bean,即我的上下文XML包含:
<context:component-scan base-package="org.example"/>
Run Code Online (Sandbox Code Playgroud)
我在下面创建了一个简单的例子来说明我的问题.
有一个Zoo是Animal对象的容器.开发人员在开发过程Zoo中不知道哪些Animal对象将被包含Zoo; AnimalSpring实例化的具体对象集在编译时是已知的,但是有各种构建配置文件导致各种Animals组,并且Zoo在这些情况下代码不得更改.
其目的Zoo是允许系统的其他部分(此处说明为ZooPatron)Animal在运行时访问对象集,而无需明确依赖某些Animals.
实际上,具体的Animal类将全部由各种Maven工件贡献.我希望能够通过简单地依赖包含这些具体Animal的各种工件来组装我的项目的分布,并且在编译时使所有内容都能正确地自动装配.
我试图通过让个人Animal依赖于这个问题来解决这个问题(不成功)Zoo,以便他们可以在Zoo期间调用注册方法@PostConstruct.这避免了Zoo明确依赖于明确的Animals 列表.
这种方法的问题在于,只有当所有s已经注册时,Zoo希望与其进行交互的客户.有一组有限的s在编译时是已知的,并且注册都发生在我的生命周期的Spring连接阶段,所以订阅模型应该是不必要的(即我不希望在运行时添加s)).AnimalAnimalAnimalZoo
不幸的是,所有的客户都Zoo依赖于Zoo.这与Animals的关系完全相同Zoo.因此,s和s 的@PostConstruct方法以任意顺序调用.下面的示例代码说明了这一点 - 在调用时,没有注册,在所有注册时都是几毫秒.AnimalZooPatron@PostConstructZooPatronAnimal
所以这里有两种类型的依赖,我在Spring中努力表达. …
目前,我在控制器和服务层之间的Autowire配置中遇到了问题.
我无法追查我的错误.
简单的日志信息
SEVERE: Exception while loading the app
SEVERE: Undeployment failed for context /OTT
SEVERE: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [com.ott.service.EmployeeService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
Run Code Online (Sandbox Code Playgroud)
下面我还给出了Controller和Service Layer代码以及dispatcher-servlet.xml
调节器
package com.ott.controller;
import com.ott.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
*
* @author SPAR
*/
@Controller
public class AdminController {
private …Run Code Online (Sandbox Code Playgroud) 问题:
我正在创建一个切入点来执行类中的方法.这个类是一个控制器类,由注释@Controller表示,因此方面不需要bean.我附加了调度程序servlet代码,方面和控制器类.有人可以确定问题是什么.
调度服务器:
<?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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config />
<context:spring-configured />
<aop:aspectj-autoproxy />
<bean id="LoggerBean" class="com.persistent.eap.aop.LoggerAspect" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" >
<property name="order" value="0" />
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="com.persistent.eap.validators.UserRegistrationValidator" />
<bean id="userRegistrationService" class="com.persistent.eap.service.impl.UserRegistrationServiceImpl" />
<bean id="userOperationsService" class="com.persistent.eap.service.impl.UserOperationsServiceImpl" />
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"/>
</bean>
<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" p:definitions="/WEB-INF/tiles-defs.xml" />
<context:component-scan base-package="com.persistent.eap.controllers" />
<context:component-scan base-package="com.persistent.eap.service" />
<context:component-scan base-package="com.persistent.eap.dao" …Run Code Online (Sandbox Code Playgroud) 我是Spring框架的新手......在google上搜索时我发现了一些在类级别上有@RequestMapping注释的例子,很少有例子在menthod级别显示
何时使用类级别RequestMapping和menthod级别RequestMapping注释...任何人都可以解释我在类级别RequestMapping和方法级别RequestMapping之间的区别.
所以我对它们的应用有点困惑:
a)班级
b)方法级别
我也找到了一些@Requestmapping类型:GET/Post,而一些例子没有类型参数.
哪种方法更好.. ??
较新的版本(> Spring 2.5)是否需要参数类型来进行请求映射?
我正在使用spring的PreAuthorize注释如下:
@PreAuthorize("hasRole('role')");
Run Code Online (Sandbox Code Playgroud)
但是,我已经将'role'定义为另一个类上的静态String.如果我尝试使用此值:
@PreAuthorize("hasRole(OtherClass.ROLE)");
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 14): Field or property 'OtherClass' cannot be found on object of type 'org.springframework.security.access.expression.method.MethodSecurityExpressionRoot'
Run Code Online (Sandbox Code Playgroud)
有没有办法使用PreAuthorize注释访问这样的静态变量?
在我的Spring MVC webapp中,我有一个用于CRUD操作的通用RESTful控制器.而且每个具体的控制器只有一个声明@RequestMapping,例如/foo.通用控制器处理所有请求/foo和/foo/{id}.
但现在我需要编写一个更复杂的CRUD控制器,它将获得额外的请求参数或路径变量,例如/foo/{date}和/foo/{id}/{date}.所以我扩展了我的通用CRUD控制器并编写了重载fetch(id, date)方法,它将处理两者{id}和{date}.那不是问题.
但是我还需要'禁用' fetch(id)从基类派生的 实现(资源不能/foo/{id}再用,只能在那里/foo/{id}/{date}).我想出的唯一想法是在我的具体控制器中覆盖此方法,将其映射到假uri上并返回null.但这看起来像是丑陋的脏黑客,因为我们暴露了一些假的资源uri,而不是禁用它.可能有更好的做法?
有任何想法吗?
//My generic CRUD controller
public abstract class AbstractCRUDControllerBean<E, PK extends Serializable> implements AbstractCRUDController<E, PK> {
@RequestMapping(method=RequestMethod.GET)
public @ResponseBody ResponseEntity<E[]> fetchAll() { ... }
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public @ResponseBody ResponseEntity<E> fetch(@PathVariable("id") PK id) { ... }
@RequestMapping(method=RequestMethod.POST)
public @ResponseBody ResponseEntity<E> add(@RequestBody E entity) { ... }
@RequestMapping(value="/{id}", method=RequestMethod.PUT) …Run Code Online (Sandbox Code Playgroud) 我已在xml配置文件中定义:
<bean id="bootstrap" class="com.package.Bootstrap"></bean>
Run Code Online (Sandbox Code Playgroud)
这很好用.
bootsrap类:
public class Bootstrap {
@PostConstruct
public void onServerStart() {
System.out.println("PRINTSSSSSSSSSSSSSSSSSSS");
}
}
Run Code Online (Sandbox Code Playgroud)
该方法被触发.
但是,如何摆脱xml部分,并将bootstrap注释为bean呢?
我有
<mvc:annotation-driven />
<context:annotation-config />
Run Code Online (Sandbox Code Playgroud)
和
<context:component-scan base-package="com.package" />
Run Code Online (Sandbox Code Playgroud)
但我想知道应用的注释应该取代:
<bean id="bootstrap" class="com.package.Bootstrap"></bean>
Run Code Online (Sandbox Code Playgroud)
我在网上和春季文档中找不到任何关于这个的信息:(
我用Spring框架开发了几年的Web应用程序.最近,我的团队的新鲜问我一个问题,Spring有@Component注释,是什么注释的真正目的@Repository,@Service,@Controller?我试着给他答案,他们之间没有区别,只是为了识别java Bean的类型.如你所知,我的解释缺乏说服力,他没有买.
所以,我想问一个问题,什么是注释的真正目的@Repository,@Service,@Controller?这些注释之间真正的区别是什么?