我现在使用Spring几个月了,我认为使用@Autowired注释的依赖注入也需要为该字段注入一个setter.
所以,我这样使用它:
@Controller
public class MyController {
@Autowired
MyService injectedService;
public void setMyService(MyService injectedService) {
this.injectedService = injectedService;
}
...
Run Code Online (Sandbox Code Playgroud)
}
但我今天试过这个:
@Controller
public class MyController {
@Autowired
MyService injectedService;
...
Run Code Online (Sandbox Code Playgroud)
}
哦惊喜,没有编译错误,启动时没有错误,应用程序运行完美...
所以我的问题是,使用@Autowired注释进行依赖注入所需的setter是什么?
我正在使用Spring 3.1.1.
我使用Newtonsoft.Json将我的对象转换为JSon文件.但我遇到DateTime的问题,在我的对象datetime字段设置为DateTime但在Json文件中我得到这种格式的datetime字段"7/30/2012 8:29:12 PM"
我需要以与对象相同的格式获取日期时间.有可能吗?我该怎么办才能获得相同的格式?
我想知道 Spring Framework是什么?为什么以及何时应该在Java Enterprise开发中使用它? 答案是"依赖注入框架".好吧,使用依赖注入框架时我们有什么优势?用setter值和/或构造函数参数描述类的想法对我来说似乎很奇怪.为什么这样?因为我们可以在不重新编译项目的情况下更改属性?这就是我们获得的一切吗?
那么,我们应该在beans.xml中描述哪些对象?所有对象还是只有几个?
最简单的答案是受欢迎的
我正以这种方式得到回应:
[{Id=1066276530, Key1=1815401000238}, {Id=1059632250, Key1=1815401000244}]
Run Code Online (Sandbox Code Playgroud)
当我迭代并将值转换为字符串时,它会抛出错误:
java.lang.Long cannot be cast to java.lang.String in Java
for (Map<String, String> map : leadIds) {
for (Map.Entry<String, String> entry : map.entrySet()) {
String applicationNumber = (String) entry.getValue();
}
}
Run Code Online (Sandbox Code Playgroud)
我想将值转换为字符串.这里有什么问题吗?
我在我的应用程序中使用spring和hibernate并使用spring事务.
所以我在方法和带有数据库查询方法的DAO层上有注释@transaction的服务层.
@Transactional(readOnly = false)
public void get(){
}
Run Code Online (Sandbox Code Playgroud)
问题是当我想在数据库中保存一个对象时,我必须@Transaction在DAO层方法结束时使用.为什么?
我想如果我有注释@ transaction,那么spring应该在完成服务方法时自动提交事务.
DAO层:
public BaseEntity saveEntity(BaseEntity entity) throws Exception {
try {
Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(entity);
session.flush();
} catch (HibernateException he) {
throw new Exception("Failed to save entity " + entity);
}
return entity;
}
Run Code Online (Sandbox Code Playgroud)
服务层:
@Transactional(readOnly = false)
public BaseEntity saveEntity(BaseEntity entity) throws Exception {
return dao.saveEntity(entity);
}
Run Code Online (Sandbox Code Playgroud)
弹簧配置:
<context:property-placeholder properties-ref="deployProperties" />
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Activate Spring Data JPA repository support -->
<jpa:repositories base-package="com" /> …Run Code Online (Sandbox Code Playgroud) 我希望能够在ArrayList<String>使用中插入元素ListIterator,但是在阅读与ListIterator类的add方法相关的文档之后我甚至感到困惑,如果我这样做的话
for(int i = 0 ; i < list.size() ; ++i)
listIterator.add( list.get(i) );
Run Code Online (Sandbox Code Playgroud)
这段代码片段对我的列表迭代器做了什么,它在哪里移动列表迭代器?
当我运行以下代码时,我得到的结果是"嗨" - :
import java.util.ArrayList;
import java.util.ListIterator;
public class ListIter {
public static void main(String[] args) {
String[] s = {"Hi", "I", "am", "Ankit"};
ArrayList<String> list = new ArrayList<>();
ListIterator<String> listIterator = list.listIterator();
for (int i = 0; i < s.length; ++i) {
listIterator.add(s[i]);
}
while (listIterator.hasPrevious()) {
listIterator.previous();
}
System.out.println(listIterator.next());
}
}
Run Code Online (Sandbox Code Playgroud)
请告诉我这个输出是如何生成的?
我正在学习restful webservices,所有教程都使用javax.ws.rs.*包.但是使用JDK 6和JDK 7,我的eclipse似乎没有认识到javax.ws包.我在这里错过了什么?
我有一个JSON对象,可能包含一些空值.我使用com.fasterxml.jackson.databind中的ObjectMapper将我的JSON对象转换为String.
private ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(object);
Run Code Online (Sandbox Code Playgroud)
如果我的对象包含任何包含值为null的字段,则该字段不包含在来自writeValueAsString的String中.我希望我的ObjectMapper给我字符串中的所有字段,即使它们的值为null.
例:
object = {"name": "John", "id": 10}
json = {"name": "John", "id": 10}
object = {"name": "John", "id": null}
json = {"name": "John"}
Run Code Online (Sandbox Code Playgroud) 我有以下的结构.我知道这看起来很奇怪,但我用这个例子模拟我们的代码.
public static class StringWrapper {
protected final String s;
@JsonValue
public String getS() {
return s;
}
public StringWrapper(final String s) {
this.s = s;
}
}
public static class StringWrapperOuter {
protected final StringWrapper s;
@JsonValue
public StringWrapper getS() {
return s;
}
public StringWrapperOuter(final StringWrapper s) {
this.s = s;
}
}
public static class POJO {
protected final List<StringWrapperOuter> data;
public List<StringWrapperOuter> getData() {
return data;
}
public POJO(final List<StringWrapperOuter> data) {
this.data = data;
} …Run Code Online (Sandbox Code Playgroud) TCP/IP与TCP和IP之间有区别吗?
我认为TCP/IP总是只是TCP和IP,但看起来TCP/IP是一个拥有更多协议而不仅仅是TCP和IP的系列.
我明白这是正确的,还是错的?