能@Component,@Repository并@Service注释互换在Spring中,还是他们提供任何特殊的功能,除了作为一个符号设备?
换句话说,如果我有一个Service类并且我将注释更改@Service为@Component,它仍然会以相同的方式运行吗?
或者注释是否也会影响类的行为和功能?
在了解了Spring @Autowired用法的问题之后,我想为另一个弹簧布线选项(@Configuration该类)创建一个完整的知识库.
假设我有一个如下所示的spring 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.0.xsd">
<import resource="another-application-context.xml"/>
<bean id="someBean" class="stack.overflow.spring.configuration.SomeClassImpl">
<constructor-arg value="${some.interesting.property}" />
</bean>
<bean id="anotherBean" class="stack.overflow.spring.configuration.AnotherClassImpl">
<constructor-arg ref="someBean"/>
<constructor-arg ref="beanFromSomewhereElse"/>
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
我该怎么用@Configuration呢?它对代码本身有什么影响吗?
前几天我开始研究这个Spring Hello World教程:http://viralpatel.net/blogs/spring-3-mvc-create-hello-world-application-spring-3-mvc/
在本教程中,使用spring-servlet.xml文件配置Spring DispatcherServlet ,这个文件:
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="net.viralpatel.spring3.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
Run Code Online (Sandbox Code Playgroud)
在这个文件中,我使用的上下文:组件扫描标签说,春天有扫描我的文件搜索的注释,因此,例如,当控制器类发现的方法是通过注解@RequestMapping("/你好")注释知道此方法处理以"/ hello"结尾的URL的HTTP请求.这很简单......
现在我的疑问与我可以在STS\Eclipse中自动构建的Spring MVC模板项目有关.
当我在STS中创建一个新的Spring MVC项目时,我的DispatcherServlet由一个名为servlet-context.xml的文件配置,该文件包含一些与前一个示例文件类似的配置.
在这个文件中,我仍然有组件扫描标记:
<context:component-scan base-package="com.mycompany.maventestwebapp" />
Run Code Online (Sandbox Code Playgroud)
但我还有另一个标签(看起来有类似的任务),这一个:
<annotation-driven />
Run Code Online (Sandbox Code Playgroud)
这两个标签有什么区别?
另一个"奇怪"的事情是,前一个示例(不使用注释驱动标记)与STS使用Spring MVC模板项目创建的项目非常相似,但是如果我从其配置中删除注释驱动标记文件项目不运行并给我以下错误:HTTP状态404 -
在堆栈跟踪中,我有:
WARN:org.springframework.web.servlet.PageNotFound - 未发现HTTP请求与URI [/ maventestwebapp /]在DispatcherServlet的映射名为 'appServlet'
但为什么?前面的示例在没有注释驱动标记的情况下运行良好,并且此控制器类非常相似.实际上,只有一种方法可以处理对"/"路径的HTTP请求
这是我的控制器类的代码:
package …Run Code Online (Sandbox Code Playgroud) 我有一个项目,我需要将@Configuration java-config类引导到XML配置中.
为此,我正在阅读我还需要包含以下bean定义(以及使用@Configuration注释的类的bean定义).
<bean class="org.springframework.config.java.process.ConfigurationPostProcessor" />
Run Code Online (Sandbox Code Playgroud)
但是,我最终收到以下错误:
Caused by: java.lang.ClassNotFoundException: org.springframework.config.java.process.ConfigurationPostProcessor
Run Code Online (Sandbox Code Playgroud)
我不得不假设我在某处丢失了一个罐子,但我的各种网络搜索还没有得到答案.任何帮助将不胜感激.谢谢.
编辑:显然,我正在阅读旧的文档,这已不再是最新的.让我备份.我的项目包含较旧的基于XML的配置.较新的代码全部使用'Java-config'.话虽如此,上下文显然是完全分开的.我想将一个java-config类'导入'到XML配置中,这样两个上下文都有那些特定的bean.有谁知道我怎么做到这一点?
我的Spring应用程序有问题,我的@Service类在应用程序启动时被创建了两次.我知道这是我的配置问题,因为我之前已经经历过,但究竟我做错了什么?
我在下面列出配置的方式有什么根本原因吗?(我已经省略了我认为无关紧要的一切)
web.xml中:
<servlet>
<servlet-name>myapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/myapp-config.xml
/WEB-INF/myapp-security.xml
/WEB-INF/myapp-mvc.xml
</param-value>
</context-param>
<listener>
<listener-class>com.myapp.servlet.MyAppContextListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)
MYAPP-servlet.xml中
<context:component-scan base-package="com.myapp" annotation-config="true" />
<mvc:annotation-driven />
Run Code Online (Sandbox Code Playgroud)
MYAPP-config.xml中
<context:component-scan base-package="com.myapp" annotation-config="true" />
<context:annotation-config />
Run Code Online (Sandbox Code Playgroud) 我试图使用spring注释在控制器中自动装配存储库.我收到错误org.springframework.data.repository.query.QueryByExampleExecutor class not found,我无法找到解决方案.
我得到的错误:
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'articleController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.payforeign.article.ArticleRepository com.payforeign.article.ArticleController.repository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'articleRepository': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/data/repository/query/QueryByExampleExecutor
Run Code Online (Sandbox Code Playgroud)
调节器
package com.payforeign.article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/service")
public class ArticleController {
@Autowired
private ArticleRepository …Run Code Online (Sandbox Code Playgroud) 我使用速度1.7与spring 3.1框架发送电子邮件.velocity用于电子邮件模板.
以下是配置
<bean id="velocityEngine"
class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<props>
<prop key="resource.loader">class</prop>
<prop key="class.resource.loader.class">
org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
</prop>
</props>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
以下是我的代码
@Component
public class EmailUtils {
@Autowired
private static VelocityEngine velocityEngine;
public static void sendMail(String subject, Map data, String template,
String toName, String toAddress) {
HtmlEmail email = new HtmlEmail();
try {
email.setHostName(hostName);
email.setSmtpPort(smtpPort);
email.setSubject(subject);
System.out.println(template +" template");
System.out.println(data +" data ");
System.out.println(velocityEngine +" velocityEngine ");
String message = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine, template, data);
System.out.println(message +" message message ");
email.setMsg(message);
email.addTo(toAddress, toName);
email.setFrom(fromAddress, …Run Code Online (Sandbox Code Playgroud) 我知道有很多类似的问题.我看了很多,但问题仍然存在.
我有一个已创建但未自动装配的服务.在项目和测试中都没有(!)手动启动(就像在这个问题中一样)
Tomcat输出说,这两个bean都被发现并创建.至少第一项服务不会注入应有的位置.我不知道第二个.
服务(接口):
public interface SchoolService {
public School getSchool(String id);
}
Run Code Online (Sandbox Code Playgroud)
服务(实施):
@Service
@Transactional
public class SchoolServiceImpl implements SchoolService {
@Autowired
private SchoolDAO schoolDAO;
public School getSchool(String id) {
//database things
return school;
}
}
Run Code Online (Sandbox Code Playgroud)
它被"召唤"的地方
public class SchoolMenu implements Serializable {
@Autowired
private SchoolService schoolService;
public SchoolMenu () {
//here schoolService is null
School school = schoolService.getSchool("id");
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序的context.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"
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-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx …Run Code Online (Sandbox Code Playgroud) I am working on a project using Spring mvc and I counter a problem that a handler is auto generated when I start the server.
Here is the code:
Controller
@Controller
@RequestMapping(value="userstory/{projectid}/{sprintid}/")
@SessionAttributes(value = "user")
public class UserstoryController {
private ISprintService sprintService;
private IUserStoryService userStoryService;
private IProjectService projectService;
private IBurnDownChartService burnDownChartService;
private ITaskService taskService;
public void setSprintService(ISprintService sprintService) {
this.sprintService = sprintService;
}
public void setUserStoryService(IUserStoryService userStoryService) {
this.userStoryService = userStoryService;
}
public void setProjectService(IProjectService projectService) {
this.projectService = projectService; …Run Code Online (Sandbox Code Playgroud) I am new to Spring MVC. I've been searching for few days for a solution to my problem, without any success.
Here is my stack:
我已经测试了自动连接的行为,以防应用程序上下文 xml 文件中缺少 context:annotation-config 元素。令我惊讶的是,它的工作原理是一样的。
所以这是我的问题:即使应用程序上下文配置文件中缺少 context:annotation-config 元素,如何在 ApplicationContext 中注册 AutowiredAnnotationBeanPostProcessor,或者还有什么机制使此配置起作用?
我使用的是 Spring 版本 3.0.6.RELEASE 这是项目 pom 文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven- v4_0_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.samples.spring</groupId>
<artifactId>spring-utility</artifactId>
<version>1.0.0.CI-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Spring Utility</name>
<url>http://www.springframework.org</url>
<description>
<![CDATA[
This project is a minimal jar utility with Spring configuration.
]]>
</description>
<properties>
<maven.test.failure.ignore>true</maven.test.failure.ignore>
<spring.framework.version>3.0.6.RELEASE</spring.framework.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin> …Run Code Online (Sandbox Code Playgroud) spring ×11
java ×6
spring-mvc ×5
annotations ×3
autowired ×2
ejb ×1
hibernate ×1
jboss ×1
spring-data ×1
velocity ×1