我们有Spring MVC应用程序.我们正在尝试将Spring安全性集成到其中.
我们编写了自定义身份验证提供程序,它将执行身份验证工作.
以下是我的自定义身份验证提供程序的代码.
public class CustomAuthenticationProvider extends DaoAuthenticationProvider {
@Autowired
private AuthenticationService authenticationService;
@Override
public Authentication authenticate(Authentication authentication) {
CustomAuthenticationToken auth = (CustomAuthenticationToken) authentication;
String username = String.valueOf(auth.getPrincipal());
String password = String.valueOf(auth.getCredentials());
try {
Users user = new User();
user.setUsername(username);
user.setPassword(PasswordUtil.encrypt(password));
user = authenticationService.validateLogin(user);
return auth;
} catch (Exception e) {
throw new BadCredentialsException("Username/Password does not match for " + username);
}
}
@Override
public boolean supports(Class<? extends Object> authentication) {
return (CustomAuthenticationToken.class.isAssignableFrom(authentication));
}
}
Run Code Online (Sandbox Code Playgroud)
这里我在以下行获取NullpointerException
user = authenticationService.validateLogin(user); …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序有2个同名的bean,但它们在不同的包中.我的Spring应用程序失败,因为它无法决定采用哪个bean.这有什么解决方案吗?Bean目前不实现特定的接口.
请参阅下面一个已编辑的异常示例:
Caused by:
org.springframework.context.annotation.ConflictingBeanDefinitionException:
Annotation-specified bean name 'dataTransferHandler' for bean class
[aaaaa.ws.handler.DataTransferHandler] conflicts with existing,
non-compatible bean definition of same name and class
[bbbbb.ws.handler.DataTransferHandler]
Run Code Online (Sandbox Code Playgroud) 我有一些自动注释的问题.我的应用程序如下所示:
这是控制器:
@Controller
public class MyController {
@Autowired
@Qualifier("someService")
private SomeService someService;
....
}
Run Code Online (Sandbox Code Playgroud)
这是一个服务层:
public interface SomeService {
...
}
@Service
public class SomeServiceImpl implements SomeService{
@Autowired
@Qualifier("myDAO")
private MyDAO myDAO;
....
}
Run Code Online (Sandbox Code Playgroud)
和DAO层:
public interface MyDAO{
....
}
@Repository
public class JDBCDAOImpl implements MyDAO {
@Autowired
@Qualifier("dataSource")
private DataSource dataSource;
....
}
Run Code Online (Sandbox Code Playgroud)
这是一个app-service.xml文件:
....
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"/>
<bean id="SomeService" class="com.service.SomeServiceImpl" />
<bean id="myDAO" class="com.db.JDBCDAOImpl" />
Run Code Online (Sandbox Code Playgroud)
所以......当我启动一个web-app时,MyController自动正确(SomeServiceImpl类对象正确注入someService字段),但someService的myDAO feild有空值(未正确注入).
你能帮我找个问题吗?
PS它很有意思,但是当我将myDAO中的"bean …
我有一组具有复杂初始化方案的类.基本上,我从我需要的接口开始,然后进行一堆调用,最后得到一个实现该接口的对象.
为了处理这个问题,我创建了一个工厂类,它可以在给定接口的情况下生成最终对象.我把这个工厂变成了一个bean,在XML中我指定我的各种服务bean通过这个工厂对象实例化,并带有一个他们将实现的接口参数.
这很好用,我完全得到了我需要的豆子.不幸的是,我想从我的控制器类访问它们,这些类是通过组件扫描发现的.我在这里使用@Autowired,看起来Spring不知道这些是什么类型的对象,并且由于@Autowired按类型工作,我是SOL.
在这里使用@Resource(name ="beanName")可以很好地工作,但是对于某些bean使用@Resource而对其他bean使用@Autowired似乎很奇怪.
有没有办法让Spring知道工厂将为每个bean创建哪个接口而不为每种类型设置不同的工厂方法?
顺便说一句,我正在使用Spring 2.5.6,否则我只是JavaConfig而忘记了它.
工厂类:
<T extends Client> T buildService(Class<T> clientClass) {
//Do lots of stuff with client class and return an object of clientClass.
}
Run Code Online (Sandbox Code Playgroud)
应用上下文:
<bean id="serviceFactoryBean" class="com.captainAwesomePants.FancyFactory" />
<bean id="userService" factory-bean="serviceFactoryBean" factory-method="buildService">
<constructor-arg value="com.captain.services.UserServiceInterface" />
</bean>
<bean id="scoreService" factory-bean="serviceFactoryBean" factory-method="buildService">
<constructor-arg value="com.captain.services.ScoreServiceInterface" />
</bean>
Run Code Online (Sandbox Code Playgroud)
我的控制器:
public class HomepageController {
//This doesn't work
@Autowired @Qualifier("userService") UserServiceInterface userService;
//This does
@Resource(name="scoreService") ScoreServiceInterface scoreService;
}
Run Code Online (Sandbox Code Playgroud) 我有一个存储库管理器来管理我的存储库.我有@Autowired来实例化我的属性,但它们总是为空.在我的xml中正确配置了bean.有什么理由吗?
public class RepositoryManager {
private static RepositoryManager instance;
private RepositoryManager()
{
}
public static RepositoryManager Instance()
{
if(instance == null)
instance = new RepositoryManager();
return instance;
}
@Autowired
private IUserRepository userRepository;
@Autowired
private IRoleRepository roleRepository;
@Autowired
private IAssetRepository assetRepository;
public IUserRepository getUserRepository() {
return userRepository;
}
public void setUserRepository(IUserRepository userRepository) {
this.userRepository = userRepository;
}
public IRoleRepository getRoleReposttory() {
return roleRepository;
}
public void setRoleReposttory(IRoleRepository roleRepository) {
this.roleRepository = roleRepository;
}
public IAssetRepository getAssetRepository() {
return assetRepository;
}
public …Run Code Online (Sandbox Code Playgroud) 我需要帮助理解背后的理念@Autowired和@Service.我有一个DAO @Service和控制器定义,@Autowired一切似乎很好,但是,我@Autowired在不同的类使用相同,然后它不起作用.
例:
服务
@Service
public class MyService {
private JdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource (DataSource myDataSource) {
this.jdbcTemplate = new JdbcTemplate(myDataSource);
}
public void testUpdate(){
jdbcTemplate.update("some query");
}
}
Run Code Online (Sandbox Code Playgroud)
调节器
package com.springtest.mywork.controller;
@Controller
@RequestMapping(value = "/test.html")
public class MyController
{
@Autowired
MyService myService;
@RequestMapping(method = RequestMethod.GET)
public String test(Model model)
{
systemsService.testUpdate();
return "view/test";
}
}
Run Code Online (Sandbox Code Playgroud)
以上一切都很好.但是,如果我想MyService在POJO中使用那么它就不起作用了.例:
package com.springtest.mywork.pojos;
public class MyPojo {
@Autowired
MyService myService;
public …Run Code Online (Sandbox Code Playgroud) 我有一个豆子:
<bean id="BasketLogic" class="efco.logic.EfcoBasketLogic" autowire="byType">
<property name="documentLogic" ref="DocumentLogic" />
<property name="stateAccess" ref="StateAccess" />
<property name="contextAccess" ref="ContextAccess" />
</bean>
<bean id="EfcoErpService" autowire="byType" class="efco.erp.service.EfcoErpServiceImpl">
<constructor-arg ref="ErpConnector"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
documentLogic,stateAccess和contextAccess上田BasketLogicImpl
我没有 <context:component-scan />
EfcoBasketLogic.java:
public class EfcoBasketLogic extends BasketLogicImpl {
@Inject
private EfcoErpService erpService;
...
...
...
}
Run Code Online (Sandbox Code Playgroud)
erpService为null,除非我提供setter.但为什么?我认为在自动装配发生的地方不需要安装器?可能是BasketLogicImpl对此负责吗?
我想将@Autowired注释用于"Aspect".我想在我的方面注入一个存储库但是当我尝试调用我的自动连接类的方法时,会发生NullPointException.
@Aspect
public class AspectSecurity {
@Autowired
private UserRepository userRepository;
@After("execution(public * dash.*.*Controller.*(..))")
public void authentication(JoinPoint jp) throws UnauthorizedException {
System.out.println("SECURITY !");
System.out.println(userRepository.findAll().toString());
}
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试@Component在我的方面类上面添加,但我有同样的错误.
如果我不使用方面类,但@Controller我可以毫无问题地调用我的存储库.
一些文档说明了使用xml文件的spring配置,但是使用spring boot我没有这些文件.
这是我的pom.xml的一部分,它调用了aspectJ插件:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<showWeaveInfo>true</showWeaveInfo>
<source>1.6</source>
<target>1.6</target>
<Xlint>ignore</Xlint>
<complianceLevel>${compiler.version}</complianceLevel>
<encoding>UTF-8</encoding>
<verbose>false</verbose>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
Run Code Online (Sandbox Code Playgroud)
这是我的Application类:
package dash;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import …Run Code Online (Sandbox Code Playgroud) 为了使这项工作,我需要什么?
interface BaseServiceInterface {
public function getRecords();
}
class BaseService implements BaseServiceInterface{
public function getRecords(){
return "bla";
}
}
class SomeOtherService{
private $baseService;
public function __construct(BaseServiceInterface $baseService){
$this->baseService = $baseService;
}
}
Run Code Online (Sandbox Code Playgroud)
我的service.yml看起来像这样:
base_service:
class: AppBundle\Service\BaseService
autowire: true
Run Code Online (Sandbox Code Playgroud)
当我尝试运行时,我得到:
无法为AppBundle\Service\SomeOtherService自动装配参数1,因为类型提示类不存在(类BaseServiceInterface不存在).
我对Spring不太熟悉,我有以下情况:
存储库类:
@Repository
public class MyRepository {
// ...
}
Run Code Online (Sandbox Code Playgroud)
使用存储库类的类:
public class MyClass extends AbstractClass {
@Autowired
private MyRepository myRepository;
//...
}
Run Code Online (Sandbox Code Playgroud)
我知道,如果我注释我MyClass用@Component,并用它与一个@Autowired,那么@Autowired MyRepository解决就好了.问题是我需要创建MyClass带反射的新实例.所以MyRepository永远不会解决,并且始终为null.
有没有办法@Autowired在这种情况下使用?
更好地解释我的情况:我有一些实现AbstractClass.在我的应用程序的设置阶段,我创建了HashMap这些实现.基本上:
{"MyClass", MyClass.class}
//...
Run Code Online (Sandbox Code Playgroud)
然后我有一个Controller映射到url 的泛型/{class}?options=...
使用{class} @PathVariable,HashMap上面和反射我能够根据给定创建一个类的实例options(这部分很重要).你们认为有更好的方法吗?
提前致谢
autowired ×10
spring ×7
spring-mvc ×4
java ×3
annotations ×1
aspectj ×1
inject ×1
php ×1
setter ×1
singleton ×1
spring-3 ×1
spring-boot ×1
symfony ×1