我正在使用Spring MVC作为我的Web应用程序.我的bean写在" spring-servlet.xml"文件中
现在我有一个类MyClass,我想使用spring bean访问这个类
在spring-servlet.xml我写的以下
<bean id="myClass" class="com.lynas.MyClass" />
Run Code Online (Sandbox Code Playgroud)
现在我需要使用它来访问它 ApplicationContext
ApplicationContext context = ??
Run Code Online (Sandbox Code Playgroud)
这样我才能做到
MyClass myClass = (MyClass) context.getBean("myClass");
Run Code Online (Sandbox Code Playgroud)
这该怎么做??
我试图在Spring中创建我的第一个bean但是在加载上下文时遇到了问题.我在src/main/resources中有一个bean的配置XML文件.
我收到以下IOException:
线程"main"中的异常org.springframework.beans.factory.BeanDefinitionStoreException:从类路径资源[src/main/resources/beans.xml]解析XML文档的IOException; 嵌套异常是
java.io.FileNotFoundException:类路径资源[src/main/resources/beans.xml]无法打开,因为它不存在
但我不明白,因为我做了以下代码测试:
File f = new File("src/main/resources/beans.xml");
System.out.println("Exist test: " + f.exists());
Run Code Online (Sandbox Code Playgroud)
这让我真实!resources在类路径中.怎么了?
我是我公司产品的最终用户.它不太适合集成到Spring中,但是我能够处理上下文并按名称检索所需的bean.但是,我仍然想知道是否有可能将bean注入到这个类中,即使该类不是由Spring本身管理的.
澄清:管理某些类MyClass生命周期的同一个应用程序也在管理Spring上下文的生命周期.Spring对MyClass的实例没有任何了解,我想知道如何将实例提供给上下文,但不能在上下文本身中创建实例.
我需要从非bean对象获取spring应用程序上下文.在SO中的另一个线程中,接受的答案建议使用单例来获取应用程序上下文. 获取Spring应用程序上下文
但是使用单例使得我的代码更加耦合并且不太可测试,许多线程中讨论的常见问题(例如单身人士的坏事)
问题是,有一种优雅的方法从非bean对象获取应用程序上下文而不使用单例吗?
我在WEB-INF/config/applicationContext.xml中使用applicationContext.xml工作web应用程序.现在我需要将一些测试工具实现为独立应用程序,它可以使用相同的applicationContext.xml,但是对于ClassPathXmlApplicationContext类的配置路径有问题.
我知道当我将applicationContext.xml复制到默认包(其中.java所在的位置)时,我可以使用ClassPathXmlApplicationContext("applicationContext.xml"),但这是必要的吗?
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AdminTool {
private Logger log = Logger.getLogger(getClass());
/** all below doesn't work - FileNotFoundException) **/
private static final ApplicationContext ac = new ClassPathXmlApplicationContext("/WEB-INF/config/applicationContext.xml");
private static final ApplicationContext ac = new ClassPathXmlApplicationContext("config/applicationContext.xml");
private static final ApplicationContext ac = new ClassPathXmlApplicationContext("../config/applicationContext.xml");
public static void main(String[] args) {
new AdminTool();
}
public AdminTool() {
log.debug(ac);
}
}
Run Code Online (Sandbox Code Playgroud) I just want to know whether controller class method is accessible from another java class.
Run Code Online (Sandbox Code Playgroud)
以下是我的控制器及其方法.
@Controller
public class TestResultUploadController {
@RequestMapping(method = RequestMethod.POST,value="/uploadTestResult")
public @ResponseBody
String uploadTestResult(String testResultBarcode,int deviceLoc) {
//some code goes here
return something;
}
Run Code Online (Sandbox Code Playgroud)
我只想从另一个java类调用这个控制器方法.我怎样才能使它工作?请建议..
如果我需要访问ApplicationContext我的bean类的构造函数,有没有办法可以用XML配置bean而不是实现ApplicationContextAware?
注意:我知道我可以使用注释驱动的配置并使用标记构造函数来完成此操作@Autowire.我特别感兴趣的是XML配置是否可行.
我有我的存储库界面
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface MyRepository extends JpaRepository<Tokens, Long>{
}
Run Code Online (Sandbox Code Playgroud)
并且我想在上面的存储库中使用的服务 impl 文件不是 @Component 或 @Service 因为它的对象是使用 new 创建的。所以它不允许在我的服务 impl 类中自动装配或注入存储库。
在这种情况下,还有其他方法可以实施吗?
我有包含方法 [void return] 的 bean 并希望在 JSP 中访问此 bean。
public class A {
public void run() {}
}
Run Code Online (Sandbox Code Playgroud)
将以下代码添加到 spring 配置文件中。
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="exposeContextBeansAsAttributes" value="true"/>
</bean>
<bean id="a" class="com.example.A"
>
</bean>
Run Code Online (Sandbox Code Playgroud)
现在在我的 JSP 页面中:
${a.run}
Run Code Online (Sandbox Code Playgroud)
但此解决方案不起作用。请帮助我在 JSP 页面上访问 spring bean。
我正在编写一个简单的代理应用程序,并且希望映射的 url 将由我的控制器处理,但其他 url(包括错误)可以转发到另一个不同的地址。因此,如果找不到资源,我会使用Filter而不是HandlerInterceptorAdapter无法调用,因为某些“资源路径处理程序”会处理它。
期待
http://localhost:8090/upload.html> Filter> > http://localhost:8092/upload.html
http://localhost:8090/files/upload>Controllerhttp://localhost:8092/files/upload
不是
http://localhost:8090/upload.html> Filter> > http://localhost:8092/upload.html
http://localhost:8090/files/upload>Controllerhttp://localhost:8092/files/upload
或者
http://localhost:8090/upload.html>>Interceptor未http://localhost:8090/error找到
http://localhost:8090/files/upload>>Filterhttp://localhost:8092/files/upload
演示
Filter我在我的子类中设置了一个WebMvcConfigurerAdapter.
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
private javax.servlet.Filter proxyFilter() {
return new OncePerRequestFilter() {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
System.out.println("[doFilterInternal]isCommitted=" + response.isCommitted() …Run Code Online (Sandbox Code Playgroud) spring ×8
java ×5
spring-mvc ×3
spring-boot ×2
autowired ×1
javabeans ×1
jsp ×1
servlets ×1
singleton ×1