我有一个组件,我想从@ComponentScan
特定的一个组件中排除@Configuration
:
@Component("foo") class Foo {
...
}
Run Code Online (Sandbox Code Playgroud)
否则,它似乎与我项目中的其他类冲突.我不完全理解碰撞,但是如果我注释掉@Component
注释,事情就像我想要的那样.但是依赖这个库的其他项目希望这个类由Spring管理,所以我想在我的项目中跳过它.
我试过用@ComponentScan.Filter
:
@Configuration
@EnableSpringConfigured
@ComponentScan(basePackages = {"com.example"}, excludeFilters={
@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value=Foo.class)})
public class MySpringConfiguration {}
Run Code Online (Sandbox Code Playgroud)
但它似乎不起作用.如果我尝试使用FilterType.ASSIGNABLE_TYPE
,我得到一个奇怪的错误,无法加载一些看似随机的类:
引起:java.io.FileNotFoundException:类路径资源[junit/framework/TestCase.class]无法打开,因为它不存在
我也尝试使用type=FilterType.CUSTOM
如下:
class ExcludeFooFilter implements TypeFilter {
@Override
public boolean match(MetadataReader metadataReader,
MetadataReaderFactory metadataReaderFactory) throws IOException {
return metadataReader.getClass() == Foo.class;
}
}
@Configuration @EnableSpringConfigured
@ComponentScan(basePackages = {"com.example"}, excludeFilters={
@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value=Foo.class)})
public class MySpringConfiguration {}
Run Code Online (Sandbox Code Playgroud)
但这似乎并没有像我想要的那样从扫描中排除组件.
我如何排除它?
我初学春天,ESP反转控制.我很困惑地理解了下面的区别
<bean id="demo" class="Demo" lazy-init="false"/>
<bean id="demo" class="Demo" lazy-init="true"/>
<bean id="demo" class="Demo" lazy-init="default"/>
Run Code Online (Sandbox Code Playgroud)
据我所知:lazy-init = false在启动时创建bean,而lazy-init = true在启动时不创建bean,而是在请求特定bean时创建bean.在这里纠正我,如果我的解释是错误的.
究竟什么是lazy-init的默认行为?它将如何实例化?
Spring框架中session和globalSession有什么区别?
<bean id="exampleBean" class="com.test.baen.ExampleBean" scope="session"/>
<bean id="exampleBean" class="com.test.baen.ExampleBean" scope="globalSession"/>
Run Code Online (Sandbox Code Playgroud)
根据我的研究,两者在Web感知Spring ApplicationContext的上下文中都是有效的.
现在,会话bean作用域将一直保留到用户会话,但是globalSession bean作用域在整个应用程序中是否可用?
是申请范围吗?
我无法理解术语"全局HTTP会话"; 它将在整个全球HTTP会话中可用吗?
你能否解释为什么Spring为下面显示的bean配置创建两个对象,因为默认情况下spring默认范围是singleton?
Spring配置在这里:
<bean id="customer" class="jp.ne.goo.beans.Customer">
<property name="custno" value="100"></property>
<property name="custName" value="rajasekhar"> </property>
</bean>
<bean id="customer2" class="jp.ne.goo.beans.Customer">
<property name="custno" value="200"></property>
<property name="custName" value="siva"></property>
</bean>
Run Code Online (Sandbox Code Playgroud) 我正在尝试设置一些代码,如果spring的请求范围可用,它将以一种方式运行,如果所述范围不可用则采用另一种方式.
有问题的应用程序是一个Web应用程序,但有一些JMX触发器和计划任务(即Quartz)也会触发调用.
例如
/**
* This class is a spring-managed singleton
*/
@Named
class MySingletonBean{
/**
* This bean is always request scoped
*/
@Inject
private MyRequestScopedBean myRequestScopedBean;
/* can be invoked either as part of request handling
or as part of a JMX trigger or scheduled task */
public void someMethod(){
if(/* check to see if request scope is available */){
myRequestScopedBean.invoke();
}else{
//do something else
}
}
}
Run Code Online (Sandbox Code Playgroud)
假设myRequestScopedBean
是请求作用域.
我知道这可以用try
- catch
围绕调用myRequestScopedBean
,例如: …
我正在努力从我的自定义库中自动装配bean,并使用gradle导入.在阅读了几个类似的主题后,我仍然无法找到解决方案.
我有Spring Boot项目,它依赖于其他项目(我的自定义库包含Components,Repositories等...).该库是一个Spring不可运行的jar,主要由域实体和存储库组成.它没有可运行的Application.class和任何属性......
当我启动应用程序时,我可以看到我的'CustomUserService'bean(来自库)正在尝试初始化,但是在其中自动装配的bean无法加载(接口UserRepository)...
错误:
com.myProject.customLibrary.configuration.CustomUserDetailsService中构造函数的参数0需要一个无法找到的类型为"com.myProject.customLibrary.configuration.UserRepository"的bean.
我甚至试图设置'Order',显式加载它(使用'scanBasePackageClasses'),使用包和标记类扫描,添加额外的'EnableJPARepository'注释但没有任何效果......
代码示例(为简单起见,包名称已更改)
package runnableProject.application;
import runnableProject.application.configuration.ServerConfigurationReference.class
import com.myProject.customLibrary.SharedReference.class
//@SpringBootApplication(scanBasePackages = {"com.myProject.customLibrary", "runnableProject.configuration"})
//@EnableJpaRepositories("com.myProject.customLibrary")
@SpringBootApplication(scanBasePackageClasses = {SharedReference.class, ServerConfigurationReference.class})
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
来自图书馆的课程:
package com.myProject.customLibrary.configuration;
import com.myProject.customLibrary.configuration.UserRepository.class;
@Service
public class CustomUserDetailsService implements UserDetailsService {
private UserRepository userRepository;
@Autowired
public CustomUserDetailsService(UserRepository userRepository) {
this.userRepository = userRepository;
}
...
package myProject.customLibrary.configuration;
@Repository
public interface UserRepository extends CustomRepository<User> {
User findByLoginAndStatus(String var1, Status var2);
...
}
Run Code Online (Sandbox Code Playgroud) multi-module spring-data-jpa spring-ioc spring-boot component-scan
我刚刚开始使用Spring IOC概念.我经常看到互联网上发现的大多数例子都使用代码来获取对象.
ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Hello hello = (Hello) appContext.getBean("hello");
Run Code Online (Sandbox Code Playgroud)
作为stackoverflow中这些问题1和2的参考.我推断,没有必要在代码中使用appContext.getBean("hello"),这被认为是不好的做法.此外,不再推荐.在这里纠正我,如果我的推论是错误的.
保持这一点,我相应地改变了我的项目.这是我的applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean id="utilClassRef" class="org.hd.derbyops.DUtils" lazy-init="false" />
<bean id="appContext" class="org.hd.derbyops.ContextProvider" lazy-init="false">
<property name="utils" ref="utilClassRef" />
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
我的contextProvider类代码
public class ContextProvider implements ApplicationContextAware {
private static ApplicationContext ctx;
/**
* Objects as properties
*/
private static DUtils utils;
public void setApplicationContext(ApplicationContext appContext)
throws BeansException {
ctx = appContext;
}
public static ApplicationContext getApplicationContext() {
return ctx; …
Run Code Online (Sandbox Code Playgroud) 作为我之前关于类加载的问题的后续跟进
我很好奇注释如何在流行的Spring框架中工作.
据我了解,可能会使用两种机制:
1.字节码上的字节码注入
Spring可以使用自己的类加载器来加载所需的类.在运行时,当加载类并且Spring确定它具有一些适当的注释时,它会注入字节码以向类添加其他属性或行为.
因此,@Controller
可以更改注释的控制器以扩展某些控制器基类,并且可以更改函数以在注释时实现路由@RequestMapping
.
@Controller
public class HelloWorldController {
@RequestMapping("/helloWorld")
public String helloWorld(Model model) {
model.addAttribute("message", "Hello World!");
return "helloWorld";
}
}
Run Code Online (Sandbox Code Playgroud)
2.用于实例化的反射
@Autowired
可以通过BeanFactory在运行时反射来读取,以处理实例化顺序并实例化已配置的属性.
public class Customer
{
private Person person;
@Autowired
public void setPerson(Person person) {
this.person = person;
}
}
Run Code Online (Sandbox Code Playgroud)
Spring注释如何真正起作用?
有时接口会使用@Component 注解进行注解。那么我的明显推理是实现此类接口的类也将被视为组件。但如果我是对的,情况就不是这样了。
那么接口上@Component注解的目的是什么呢?
java spring dependency-injection inversion-of-control spring-ioc
当我使用Mockito和Junit编写测试用例时,我正在使用@InjectMocks
该类进行测试.在项目的其他部分,我也看到@Autowired
正在用于测试的类.
我什么时候可以使用@InjectMocks
和@Autowired
?当我们尝试将它们与要测试的类一起使用时,两者之间有什么区别?
spring-ioc ×10
spring ×9
java ×7
spring-boot ×2
spring-mvc ×2
annotations ×1
classloader ×1
java-ee ×1
mockito ×1
multi-module ×1
singleton ×1