情况:
我有一个带有java.util.Date类型变量的persistable类:
import java.util.Date;
@Entity
@Table(name = "prd_period")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Period extends ManagedEntity implements Interval {
@Column(name = "startdate_", nullable = false)
private Date startDate;
}
Run Code Online (Sandbox Code Playgroud)
DB中的对应表:
CREATE TABLE 'prd_period' (
'id_' bigint(20) NOT NULL AUTO_INCREMENT,
...
'startdate_' datetime NOT NULL
)
Run Code Online (Sandbox Code Playgroud)
然后我将Period对象保存到DB:
Period p = new Period();
Date d = new Date();
p.setStartDate();
myDao.save(p);
Run Code Online (Sandbox Code Playgroud)
在那之后如果我试图从DB中提取我的对象,它将返回时间戳类型的变量startDate - 并且我尝试使用的所有地方等于(...)返回false.
问题:有没有办法强制Hibernate将日期作为java.util.Date类型的对象而不是Timestamp返回而不对每个这样的变量进行显式修改(例如,它必须能够正常工作,而不需要对java.util的现有变量进行显式修改.日期类型)?
注意:
我找到了许多显式解决方案,其中使用了注释或修改了setter - 但我有许多带有Date变量的类 - 所以我需要一些集中式解决方案,下面描述的所有内容都不够好:
使用注释@Type: - 将返回java.sql.Date
@Column
@Type(type="date")
private Date startDate;
Run Code Online (Sandbox Code Playgroud)使用注释@Temporal(TemporalType.DATE) - …
情况:我有一个带有@Autowired注释的属性的类:
public class MyClass {
@Autowired
protected MyAutoWiredBean myAutowiredBean;
}
Run Code Online (Sandbox Code Playgroud)
是否有可能将这个bean连接成可选的,即如果在某个配置文件中定义了这样的bean - 连接它,但是如果没有定义这样的bean - 只需继续工作而不抛出:
org.springframework.beans.factory.BeanCreationException:
Could not autowire field: protected MyAutoWiredBean...;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No matching bean of type [com.mypackage.MyAutoWiredBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
Run Code Online (Sandbox Code Playgroud) 序幕.我想问一下,如何在网络上创建桌面屏幕共享解决方案,但后来发现有很多这样的问题.用几个字符串来回答这个任务非常复杂.所以我花了一些时间试图找到合适的解决方案.似乎我找到了一个 - 只是想分享它.
初始任务:我们有一个Web应用程序.我们需要找到一种方式让用户与任何其他用户共享他的屏幕.实现于:Win 7 x64,Java,Wowza-3.5.0.
我们怎样才能做到这一点?
情况:我有@Service注释的服务实现类,可以访问属性文件.
@Service("myService")
public class MySystemServiceImpl implements SystemService{
@Resource
private Properties appProperties;
}
Run Code Online (Sandbox Code Playgroud)
属性对象通过config-file配置.applicationContext.xml中
<util:properties id="appProperties" location="classpath:application.properties"/>
Run Code Online (Sandbox Code Playgroud)
我想测试一下这种实现的方法.
问题:如何从测试类访问MySystemServiceImpl-object,以便正确初始化Properties appProperties?
public class MySystemServiceImplTest {
//HOW TO INITIALIZE PROPERLY THROUGH SPRING?
MySystemServiceImpl testSubject;
@Test
public void methodToTest(){
Assert.assertNotNull(testSubject.methodToTest());
}
}
Run Code Online (Sandbox Code Playgroud)
我不能简单地创建新的MySystemServiceImpl - 比使用appProperties的方法抛出NullPointerException.而且我不能直接在对象中注入属性 - 没有合适的setter方法.
只需在此处输入正确的步骤(感谢@NimChimpsky的回答):
我在test/resources目录下复制了application.properties.
我在test/resources目录下复制了applicationContext.xml.在应用程序上下文中,我添加了新的bean(应用程序属性的定义已经在这里):
<bean id="testSubject" class="com.package.MySystemServiceImpl">
Run Code Online (Sandbox Code Playgroud)我用这样的方式修改了测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/applicationContext.xml"})
public class MySystemServiceImplTest {
@Autowired
MySystemServiceImpl testSubject;
}
Run Code Online (Sandbox Code Playgroud)这就是诀窍 - 现在在我的测试类中可以使用全功能对象
情况:我有一个类 MyController 与一些外部 WebServices 一起工作。
public class MyController {
private String integrationWebServiceURL;
}
Run Code Online (Sandbox Code Playgroud)
此类 Web 服务 URL 在描述符(applicationContext.xml)中的配置控制器 bean 期间传递
<bean id="myController" class="com.mypath.MyController">
<property name="integrationWebServiceURL" value="${integration.web.service.url}"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
该值是动态的,实际值存储在属性文件application.properties 中
integration.web.service.url=${pom.integration.web.service.url}
Run Code Online (Sandbox Code Playgroud)
但这不是结束 - 真正的值存储在 Maven 项目文件(pom.xml)中,过滤 = true。
<pom.integration.web.service.url>http://mywebservices.com</pom.integration.web.service.url>
Run Code Online (Sandbox Code Playgroud)
因此,当我们使用mvn install 时,将 pom.xml 中的测试值复制到 application.properties 中的适当占位符,然后对我的类进行测试就可以了。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/applicationContext.xml"})
public class MyControllerTest {
}
Run Code Online (Sandbox Code Playgroud)
问题:我需要从 IDE 启动我的测试才能使用不同的设置并使用 IDE 的调试功能。但是,如果我在没有初步 Maven 构建的情况下简单地从 IDE 启动此测试 - 比我的 Web 服务地址将简单地从 application.properties 获取并等于“${pom.integration.web.service.url}”(例如,Maven 过滤过程不会测试前不工作)。如何调整 Maven、Spring 或 …