如果我使用@Bean声明一个类然后对该类进行组件扫描,那么spring将通过调用它的构造函数并注入构造函数args并注入标有@Inject的任何字段来实例化该类.为简单起见,我们称之为春季汽车制造.
我不喜欢组件扫描,并希望完全避免它(我不想讨论我不喜欢它的原因).我想使用@Configuration对象,但仍然希望我可以使用自动构建功能.是否可以调用spring来自动构建我的对象,而不是显式地必须传递我的@Configuration对象中的所有构造函数参数?
让我们假设我有一个bean:
public class MyServiceImpl implements MyService {
public MyServiceImpl(Dependency1 d1, Dependency d2) { ... }
....
}
Run Code Online (Sandbox Code Playgroud)
我可以像这样定义一个配置对象:
@Configuration
public class MyConfiguration {
// lets assume d1 and d2 are defined in another @Configuration
@Inject
Dependency1 d1;
@Inject
Dependency2 d2;
@Bean
public MyService myService() {
// I dislike how I have to explicitly call the constructor here
return new MyServiceImpl(d1, d2);
}
}
Run Code Online (Sandbox Code Playgroud)
但是现在,我明确地必须自己调用MyServiceImpl构造函数,因此随着时间的推移,我必须不断更新它.
我希望我可以声明一个抽象方法,以便可以进行弹簧自动构建:
@Configuration
public abstract class MyConfiguration {
@Bean
public abstract MyServiceImpl myService();
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用.有没有办法可以在 …
我正在使用Spring,因为我有 user.jsp
user.jsp有三个独立的分歧:1.个人,2.教育,3.奖励.每个部分都有我创建的不同形式.jsp.
现在我想将这三种形式包含user.jsp在一起并使用一个Controller显示模型.
这是我的控制器类代码:
@Controller
@RequestMapping(value="profile")
public class UserProfileController {
@RequestMapping(value="user", method=RequestMethod.GET)
public String user(Model model) throws Exception {
model.addAttribute("profile", new PersonalForm());
return "profile/user";
}
Run Code Online (Sandbox Code Playgroud)
这是我的Personal.jsp文件(所有剩余文件都相同,但名称不同)
那么如何将这三个jsp包含进去user.jsp呢?其实我正在尝试,但Eclipse显示错误.以下是我的错误代码user.jsp:
在预期的路径/ EClass/WebContent/WEB-INF/pages/profile/profile/professional.jsp中找不到片段"profile/professional.jsp"
那么请帮助我如何包含以及如何使用单个控制器?
在我的Web应用程序中,我没有使用applicationContext.xml.我怎样才能获得带@Service注释类的bean ?
如果我applicationContext.xml在我的Web应用程序中使用,我必须applicationContext.xml每次都加载以获取带@Service注释的类的bean .
我用这种方式
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext);
ServerConnection con = (ServerConnection ) ctx.getBean("con");
Run Code Online (Sandbox Code Playgroud)
我的服务类将是,
@Service or @Service("con")
public class ServerConnection {
private TServerProtocol tServerProtocol;
private URI uri;
public TServerProtocol getServerConnection(){
System.out.println("host :"+host+"\nport :"+port);
try {
uri = new URI("tcp://" + host + ":" + port);
} catch (URISyntaxException e) {
System.out.println("Exception in xreating URI Path");
}
tServerProtocol = new TServerProtocol(new Endpoint(uri));
return tServerProtocol;
}
}
Run Code Online (Sandbox Code Playgroud)
有没有其他方法来获得这个类 …
我已经将Spring注释添加到我的代码中但是当通过visual vm连接时,方法"myExample()"没有显示在JMX bean列表中
我的代码:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.stereotype.Component;
@Component
@ManagedResource
public class MyClass {
@Autowired
private Example exampleService;
@ManagedAttribute
public String myExample() {
return exampleService.getSomething().toString();
}
}
Run Code Online (Sandbox Code Playgroud)
知道为什么会这样吗?
我创建了一个元注释@EmbeddedMongoDBUnitTest,它激活了两个配置文件,用于基于弹簧的单元测试.基本设置有效:
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@ActiveProfiles({"embeddedMongoDB", "embeddedMongoDBUnitTest"})
public @interface EmbeddedMongoDBUnitTest {
}
@RunWith(SpringJUnit4ClassRunner.class)
@EmbeddedMongoDBUnitTest
@ContextConfiguration(...)
public class WorkingTest {
...
}
Run Code Online (Sandbox Code Playgroud)
现在,当尝试使用测试类本身上的另一个@ActiveProfiles注释激活另一个配置文件时,@ EmbeddedMongoDBUnitTest中的配置文件不再被激活:
@RunWith(SpringJUnit4ClassRunner.class)
@EmbeddedMongoDBUnitTest
@ActiveProfiles({"h2IntegrationTests"})
@ContextConfiguration(...)
public class NotWorkingTest {
...
}
Run Code Online (Sandbox Code Playgroud)
有没有理由说这不起作用,或者这是春季测试代码中的错误?
如何仅通过使用弹簧注释在测试类中自动装配服务
当我尝试以下错误时,在UserServiceImp类中使用@Service批注
2014-12-20 15:35:52错误TestContextManager:334-允许TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@5af97850]准备测试实例[com.amsb.bariz.base.test]时捕获了异常。 UserTest @ 4520ebad] org.springframework.beans.factory.BeanCreationException:创建名称为'com.amsb.bariz.base.test.UserTest'的bean时出错:自动连接依赖项的注入失败;嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动连线字段:public com.amsb.bariz.base.service.UserService com.amsb.bariz.base.test.UserTest.userService; 嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有找到类型为[com.amsb.bariz.base.service.UserService]的合格Bean作为依赖项:期望至少有1个bean符合此依赖项的自动装配条件。依赖项注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
我的服务等级是
package com.amsb.bariz.base.service.imp;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.amsb.bariz.base.dao.UserDao;
import com.amsb.bariz.base.dao.UserRoleDao;
import com.amsb.bariz.base.entity.User;
import com.amsb.bariz.base.entity.UserRole;
import com.amsb.bariz.base.service.UserService;
@Service("userService")
public class UserServiceImp implements UserService {
@Autowired
private UserDao userDao;
@Autowired
private UserRoleDao userRoleDao;
public void register(User user) {
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
user.setPassword(passwordEncoder.encode(user.getPassword()));
Calendar calendar = Calendar.getInstance();
java.util.Date now = calendar.getTime();
Date dateNow …Run Code Online (Sandbox Code Playgroud) 我有spring安全应用程序,希望启用注释安全性(授权前和授权后).我也有小样本应用程序,我已经实现了它.一切正常.但是将配置移动到主应用程序失败了.控制台中没有错误.但注释不起作用.看起来,他们根本就没有被贬低.所有配置和组件版本完全相同.
有
<security:global-method-security secured-annotations="enabled" />
Run Code Online (Sandbox Code Playgroud)
安全上下文和servlet上下文中的记录.但是@Controller方法没有@Service方法在主应用程序中使用注释保护.
我该怎么调试呢?
解决了!
从<global-method-security secured-annotations ="enabled"/>切换到pre/post注释后工作正常.
spring authorization annotations spring-security spring-annotations
我读过这篇stackoverflow帖子 @Secured和@PreAuthorize在spring security 3中的区别是什么? 但是,我还不清楚两者在安全方面有什么大不同?与@Secured相比,在什么情况下我们应该选择@PreAuthorize?
我正在尝试使用aliasFor批注使用spring的Meta批注为springs RequestParam创建自定义批注
只需“扩展/替换”
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
@AliasFor("name")
String value() default "";
----
}
Run Code Online (Sandbox Code Playgroud)
加上我的注释
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface QueryParam {
@AliasFor(annotation = RequestParam.class, attribute = "name")
String name() default "";
@AliasFor(annotation = RequestParam.class, attribute = "required")
boolean required() default false;
@AliasFor(annotation = RequestParam.class, attribute = "defaultValue")
String defaultValue() default ValueConstants.DEFAULT_NONE;
}
Run Code Online (Sandbox Code Playgroud)
这样就抛出了异常
org.springframework.core.annotation.AnnotationConfigurationException: @AliasFor declaration on attribute [name] in annotation [package.QueryParam] declares an alias for attribute [name] in meta-annotation [org.springframework.web.bind.annotation.RequestParam] which is not meta-present. …Run Code Online (Sandbox Code Playgroud) 我想做一个使用@Async注解以异步方式写入数据库的方法。
我用注解标记了该类@EnableAsync:
@EnableAsync
public class FacialRecognitionAsyncImpl {
@Async
public void populateDataInPushQueue(int mediaId, int studentId) {
//myCode
}
}
Run Code Online (Sandbox Code Playgroud)
在调用该populateDataInPushQueue方法时,写操作应在另一个线程中执行,并且流程应从我正在调用此方法的类中继续。但这没有发生,程序执行正在等待此方法完成。
spring ×10
java ×7
annotations ×3
junit ×2
spring-boot ×2
spring-mvc ×2
asynchronous ×1
autowired ×1
jsp ×1
spring-jmx ×1
spring-test ×1