我正在学习春天3,我似乎没有把握背后的功能<context:annotation-config>和<context:component-scan>.
根据我的阅读,他们似乎处理不同的注释(@ Required,@ Autowired etc vs @Component,@ Repository,@ Service等),但也从我读过的内容中注册了相同的bean后处理器类.
为了让我更加困惑,有一个@Required属性@Autowired.
有人可以对这些标签有所了解吗?什么是相似的,什么是不同的,一个被另一个取代,它们相互完成,我需要其中一个,两者都有吗?
我正在阅读spring 3.0.x参考文档以了解Spring Autowired注释:
我无法理解下面的例子.我们是否需要在XML中执行某些操作才能使用它?
例1
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
例2
public class MovieRecommender {
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public void prepare(MovieCatalog movieCatalog,
CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
如何通过自动装配两个类来实现相同的接口并使用相同的类?
例:
class Red implements Color
class Blue implements Color
class myMainClass{
@Autowired
private Color color;
draw(){
color.design();
}
}
Run Code Online (Sandbox Code Playgroud)
将调用哪种设计方法?如何确保调用Red类的设计方法而不是Blue?
我正在从Spring 2.5迁移到Spring 3.
他们介绍了<mvc:annotation-driven />哪些黑魔法.这应该只在servlet配置文件中声明.
在Spring 2.5中,我刚刚使用<context:annotation-config />和<context:component-scan base='...'/>标记声明application-context.xml和调度程序servlet配置XML以及适当的基础包进行扫描.
所以,我不知道是什么样的区别mvc:annotation-driven,并context:annotation-config在servlet配置标签和我有什么可以消除在春季3配置文件?
我正在使用Spring Framework 4.0.7,以及MVC和Rest
我可以和平地工作:
@Controller ResponseEntity<T>例如:
@Controller
@RequestMapping("/person")
@Profile("responseentity")
public class PersonRestResponseEntityController {
Run Code Online (Sandbox Code Playgroud)
用这个方法(只是为了创建)
@RequestMapping(value="/", method=RequestMethod.POST)
public ResponseEntity<Void> createPerson(@RequestBody Person person, UriComponentsBuilder ucb){
logger.info("PersonRestResponseEntityController - createPerson");
if(person==null)
logger.error("person is null!!!");
else
logger.info("{}", person.toString());
personMapRepository.savePerson(person);
HttpHeaders headers = new HttpHeaders();
headers.add("1", "uno");
//http://localhost:8080/spring-utility/person/1
headers.setLocation(ucb.path("/person/{id}").buildAndExpand(person.getId()).toUri());
return new ResponseEntity<>(headers, HttpStatus.CREATED);
}
Run Code Online (Sandbox Code Playgroud)
返回一些东西
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public ResponseEntity<Person> getPerson(@PathVariable Integer id){
logger.info("PersonRestResponseEntityController - getPerson - id: {}", id);
Person person = personMapRepository.findPerson(id);
return new ResponseEntity<>(person, HttpStatus.FOUND);
}
Run Code Online (Sandbox Code Playgroud)
工作良好
我可以这样做:
@RestController(我知道它与@Controller+ …我在web应用程序中一起使用jsf和spring.我已经在一个使用注释@Configuration, @ComponentScan等的配置类中配置了数据源和会话工厂.我的项目中没有任何applicationContext.xml文件,因为我正在处理Configuration类中的每个上下文xml条目.测试用例成功运行但是当我部署我的Web应用程序时,它给了我错误
java.lang.IllegalStateException:找不到WebApplicationContext:没有注册ContextLoaderListener?
现在如果我在web.xml中给出listener类,
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)
它给了我错误,
找不到/WEB-INF/applicationContext.xml
按照的文件ContextLoaderListener,这是真的,如果我不给contextConfigLocation在参数web.xml明确,它会搜索名为默认的Spring上下文文件applicationContext.xml中web.xml.现在,如果我不想使用spring上下文文件并使用注释进行所有配置,我该怎么办?我应该如何注册监听器类,ContextLoaderListener以便不使用xml文件和仅使用注释,我能够使用spring和jsf运行我的Web应用程序?
我的Spring 3应用程序中的每个视图都有一组可以依赖的属性.所以每个控制器的第一行是这样的:
ControllerHelper.addDefaultModel(model, personManager, request);
Run Code Online (Sandbox Code Playgroud)
在那里我会补充
imagesHost)这一切都允许每个视图显示登录用户的名称,轻松引用图像位置,语言列表和一些有关该站点的整体统计信息.
所以问题是,控制器模型对象是存储所有数据的最佳位置还是有一个更方便的地方,使得视图访问这些信息一样容易?
其次,我真的很想不必将ControllerHelper上面的行作为每个控制器的第一行.它实际上并不总是第一行,有时我首先检查是否需要在该控制器中重定向,因为我不想浪费资源填充模型.也许一个过滤器或注释或一些春天的回调机制可以确保ControllerHelper代码被称为后,控制器完成,但正确的前视图呈现,跳过此是否返回重定向?
我有下面的注释.
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}
Run Code Online (Sandbox Code Playgroud)
public class SomeAspect{
@Around("execution(public * *(..)) && @annotation(com.mycompany.MyAnnotation)")
public Object procede(ProceedingJoinPoint call) throws Throwable {
//Some logic
}
}
Run Code Online (Sandbox Code Playgroud)
public class SomeOther{
@MyAnnotation("ABC")
public String someMethod(String name){
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的类中我在@MyAnnotation中传递" ABC " .现在我如何在SomeAspect.java类的procede方法中访问" ABC "值?
谢谢!
我正在使用Spring 4.0.7
我通过JavaConfig对配置Spring MVC进行了研究.
实际上直到昨天我已经看到使用这两个选项的两种配置
注意:(2)是两个类,一个用于扩展,另一个用于实现
我正在使用(2)因为我找到了很多例子,我可以配置转换器,格式化程序,资源处理程序等...
但是在最近的几天里,我试图帮助StackOverflow上的一个问题,我确实认识到(1)存在..我在Google上做了一些关于(1)的概述,并且存在一些与(1)一起工作的例子
我的问题是这篇文章的标题如何描述.
谢谢
EJB在3.x版本中实现了许多改进,Spring也是常用的,而版本3是一个很好的选择.
网上有很多文章,但没有关于ejb3x与spring3x的确切比较.你对它们有什么想法,在现实世界的例子中哪个更好的条件?
例如,我们想要分离数据库和服务器,这意味着我们的应用程序将在服务器上,我们的数据库将在另一个服务器上.. EJB远程控制与Cluster4Spring等?
做每一个@Annotation总是好的?配置永远不需要?
我正在使用Spring 4.0.7
关于Spring MVC,出于研究目的,我有以下内容:
@RequestMapping(value="/getjsonperson",
method=RequestMethod.GET,
produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Person getJSONPerson(){
logger.info("getJSONPerson - getjsonperson");
return PersonFactory.createPerson();
}
@RequestMapping(value="/getperson.json", method=RequestMethod.GET)
public @ResponseBody Person getPersonJSON(){
logger.info("getPerson - getpersonJSON");
return PersonFactory.createPerson();
}
Run Code Online (Sandbox Code Playgroud)
每个都工作正常,观察JSON,有和没有扩展:
对于XML也是如此
@RequestMapping(value="/getxmlperson",
method=RequestMethod.GET,
produces=MediaType.APPLICATION_XML_VALUE
)
public @ResponseBody Person getXMLPerson(){
logger.info("getXMLPerson - getxmlperson");
return PersonFactory.createPerson();
}
@RequestMapping(value="/getperson.xml", method=RequestMethod.GET)
@ResponseBody
public Person getPersonXML(){
logger.info("getPerson - getpersonXML");
return PersonFactory.createPerson();
}
Run Code Online (Sandbox Code Playgroud)
每个都工作正常,观察XML,有和没有扩展:
现在关于Restful我有以下内容:
@RequestMapping(value="/person/{id}/",
method=RequestMethod.GET,
produces={MediaType.APPLICATION_JSON_VALUE,
MediaType.APPLICATION_XML_VALUE})
public ResponseEntity<Person> getPersonCustomizedRestrict(@PathVariable Integer id){
Person person = personMapRepository.findPerson(id); …Run Code Online (Sandbox Code Playgroud) spring-3 ×10
spring ×9
java ×5
spring-mvc ×5
spring-4 ×3
annotations ×1
autowired ×1
comparison ×1
ejb-3.0 ×1
ejb-3.1 ×1
java-ee-7 ×1
rest ×1
spring-aop ×1