最近在我们的团队中,我们开始讨论在代码中使用spring注释来定义spring依赖项.目前我们使用context.xml来定义依赖项.你能不能给我一些线索,哪一种更好用?
编辑:我知道这对于更一般的问题来说似乎是一个重复的问题,但我对注释与配置对依赖注入的影响感兴趣,我相信它会比一般问题有不同的答案和态度.
我有一个Java库我正在考虑移植到C#.Java库广泛使用注释(在构建时和运行时).
我从未使用过C#属性,但是要明白它们是Java注释的粗略等价物.
如果我使用属性来替换注释继续使用端口,我需要知道什么?什么会是一样的?不同?什么会咬我?
使用XML配置的Spring bean工厂,我可以使用不同的参数轻松实例化同一个类的多个实例.我怎么能用注释做同样的事情?我想要这样的东西:
@Component(firstName="joe", lastName="smith")
@Component(firstName="mary", lastName="Williams")
public class Person { /* blah blah */ }
Run Code Online (Sandbox Code Playgroud) 您将如何在以下示例代码中配置注释?我只想坚持使用JPA注释,并避免使用Hibernate特定的依赖项.以下代码是否正确?
@Entity
public class RefExample extends RefData {
}
Run Code Online (Sandbox Code Playgroud)
(这些类将有多个版本,RefSomeOtherExample等,每个类有一个db表.有些可能会添加其他字段(列),但大多数只会使用从"RefData"基类继承的基本字段.)
基类:
@Entity
public abstract class RefData {
private long id;
private String code;
private String desc;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(unique = true, nullable = false)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(unique = true, nullable = false, length=8)
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Column(unique = true, nullable …Run Code Online (Sandbox Code Playgroud) RetentionPolicy.CLASS和之间有什么实际区别RetentionPolicy.RUNTIME?
看起来两者都记录在字节码中,无论如何都可以在运行时访问它们.
我想知道如何用Spring 3注释实现简单的工厂模式.我在文档中看到,您可以创建调用工厂类并运行工厂方法的bean.我想知道这是否可能只使用注释.
我有一个目前正在呼叫的控制器
MyService myService = myServiceFactory.getMyService(test);
result = myService.checkStatus();
Run Code Online (Sandbox Code Playgroud)
MyService是一个名为checkStatus()的方法的接口.
我的工厂类看起来像这样:
@Component
public class MyServiceFactory {
public static MyService getMyService(String service) {
MyService myService;
service = service.toLowerCase();
if (service.equals("one")) {
myService = new MyServiceOne();
} else if (service.equals("two")) {
myService = new MyServiceTwo();
} else if (service.equals("three")) {
myService = new MyServiceThree();
} else {
myService = new MyServiceDefault();
}
return myService;
}
}
Run Code Online (Sandbox Code Playgroud)
MyServiceOne类如下所示:
@Autowired
private LocationService locationService;
public boolean checkStatus() {
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,locationService变量为alwasy null.我相信这是因为我在工厂里自己创造了这些物品并且没有发生自动装配.有没有办法添加注释才能使其正常工作?
谢谢
我正在尝试使用Spring为webapp自动装配一些bean(用于依赖注入).一个控制器bean包含另一个bean,而另一个bean又拥有另一组bean的hashmap.目前,地图只有一个条目.当我在tomcat中运行并调用该服务时,我得到一个错误,说第二个bean(保存在控制器中)不是唯一的
No unique bean of type [com.hp.it.km.search.web.suggestion.SuggestionService] is defined: expected single matching bean but found 2: [suggestionService, SuggestionService]
Run Code Online (Sandbox Code Playgroud)
我无法看到我在两次定义bean的位置,但是我是Spring的新手并且自动装配,所以我可能会遗漏一些基本的东西.下面列出的xml和2类的源代码......
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.hp.it.km.search.web.suggestion" />
<mvc:annotation-driven />
<context:annotation-config />
<bean id="SuggestionController" class="com.hp.it.km.search.web.suggestion.SuggestionController">
<property name="service">
<ref bean="SuggestionService" />
</property>
</bean>
<bean id="SuggestionService" class="com.hp.it.km.search.web.suggestion.SuggestionService">
<property name="indexSearchers">
<map>
<entry key="KMSearcher"> <ref bean="KMSearcherBean"></ref></entry>
</map>
</property>
</bean>
<bean id="KMSearcherBean" class="com.hp.it.km.search.web.suggestion.SuggestionIndexSearcher">
<constructor-arg index="0" value="KMSearcher" />
<constructor-arg index="1" value="C://dev//workspace//search-restful-webapp//src//main//resources//indexes//keyword" />
</bean>
Run Code Online (Sandbox Code Playgroud)
带有自动控制器和服务bean的类asscoaites在这里......
@Controller
public class …Run Code Online (Sandbox Code Playgroud) spring annotations dependency-injection spring-mvc autowired
我创建了一个接口,有大约30种方法,并在30个类中实现.
我想在每个实现中添加@Override,但我不想手动完成.
IntelliJ如何帮助我?
界面看起来像这样:
public interface PreviewObjectTests {
void testGetName();
void testGetType();
//... 30 similar methods
}
Run Code Online (Sandbox Code Playgroud)
实施代码:
public class PreviewObjectAccountTest implements PreviewObjectTests {
//I want to add @Override here!
@Test public void testGetName() {
assertThat(...);
}
//I want to add @Override here!
@Test public void testGetType() {
assertThat(...);
}
//...30 similar methods
}
Run Code Online (Sandbox Code Playgroud) 两者有什么区别?两者似乎都意味着该值可以为null并且应该相应地处理,即检查为null.
更新: 上面的两个注释是JSR-305/FindBugs的一部分:http: //findbugs.sourceforge.net/manual/annotations.html
annotations ×10
java ×7
spring ×4
attributes ×1
autowired ×1
c# ×1
findbugs ×1
gson ×1
hibernate ×1
import ×1
jpa ×1
spring-mvc ×1