我想我误解了在@ManyToOne关系背景下级联的意义.
案子:
public class User {
@OneToMany(fetch = FetchType.EAGER)
protected Set<Address> userAddresses;
}
public class Address {
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
protected User addressOwner;
}
Run Code Online (Sandbox Code Playgroud)
这是什么意思cascade = CascadeType.ALL?例如,如果我从数据库中删除某个地址,我添加的事实如何cascade = CascadeType.ALL影响我的数据(User我猜)?
在我的java进程中,我使用以下spring配置连接到MySql:
@Configuration
@EnableTransactionManagement
@PropertySources({ @PropertySource("classpath:/myProperties1.properties"), @PropertySource("classpath:/myProperties2.properties") })
public class MyConfiguration {
@Autowired
protected Environment env;
/**
* @return EntityManagerFactory for use with Hibernate JPA provider
*/
@Bean(destroyMethod = "destroy")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setJpaVendorAdapter(jpaVendorAdapter());
em.setPersistenceUnitManager(persistenceUnitManager());
return em;
}
/**
*
* @return jpaVendorAdapter that works in conjunction with the
* persistence.xml
*/
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setDatabase(Database.valueOf(env.getProperty("jpa.database")));
vendorAdapter.setDatabasePlatform(env.getProperty("jpa.dialect"));
vendorAdapter.setGenerateDdl(env.getProperty("jpa.generateDdl", Boolean.class, false));
vendorAdapter.setShowSql(env.getProperty("jpa.showSql", Boolean.class, false));
return vendorAdapter;
}
@Bean …Run Code Online (Sandbox Code Playgroud) 在java-spring网络应用程序中,我希望能够动态注入bean.例如,我有一个具有2种不同实现的接口:
在我的应用程序中,我正在使用一些属性文件来配置注入:
#Determines the interface type the app uses. Possible values: implA, implB
myinterface.type=implA
Run Code Online (Sandbox Code Playgroud)
我的注入实际上有条件地在属性文件中的属性值上加载.例如,在这种情况下myinterface.type = implA,只要我注入MyInterface,将注入的实现将是ImplA(我通过扩展条件注释完成了).
我希望在运行时 - 一旦属性发生更改,将发生以下情况(无需重新启动服务器):
myinterface.type=implBImplB将被注入到使用MyInterface的地方我想要刷新我的上下文,但这会产生问题.我想可能会使用setter进行注入,并在重新配置属性后重新使用这些setter.是否有这种要求的工作实践?
有任何想法吗?
UPDATE
正如一些人所建议我可以使用一个工厂/注册表来保存两个实现(ImplA和ImplB)并通过查询相关属性返回正确的实现.如果我这样做,我还有第二个挑战 - 环境.例如,如果我的注册表看起来像这样:
@Service
public class MyRegistry {
private String configurationValue;
private final MyInterface implA;
private final MyInterface implB;
@Inject
public MyRegistry(Environmant env, MyInterface implA, MyInterface ImplB) {
this.implA = implA;
this.implB = implB;
this.configurationValue = env.getProperty("myinterface.type");
}
public MyInterface getMyInterface() {
switch(configurationValue) {
case "implA":
return implA; …Run Code Online (Sandbox Code Playgroud) 在我的.NET MVC 4中,我添加了一个全局过滤器以保护我的控制器.这是使用:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new System.Web.Mvc.AuthorizeAttribute());
}
Run Code Online (Sandbox Code Playgroud)
这是从我的Global.cs类调用的.
我的Web.config文件包含标准配置:
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
Run Code Online (Sandbox Code Playgroud)
我的登录操作被装饰,[AllowAnonymous]以允许匿名用户登录.
到目前为止,一切都很好.这是我的登录操作:
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
Run Code Online (Sandbox Code Playgroud)
现在,我想添加一个重置密码页面,就像匿名用户可以使用登录页面一样.我创建了我的动作(在Login动作的同一控制器下)并用装饰装饰它[AllowAnonymous]:
[AllowAnonymous]
public ActionResult ResetPasswordForUser(string message = "")
{
ViewBag.message = message;
return View();
}
Run Code Online (Sandbox Code Playgroud)
然后创建相应的视图.
我将此链接添加到我的登录页面:
@Html.ActionLink("Forgot your password?", "ResetPasswordForUser", "Account")
Run Code Online (Sandbox Code Playgroud)
在运行时,当我使用匿名用户单击此链接时,我会进入ResetPasswordForUser操作,但是当返回视图时,将调用Login操作,并且我永远无法实际访问所需的视图.出于某种原因,即使我使用[AllowAnonymous]装饰,我的请求也会被截获.
我在这里错过了什么吗?
提前致谢
UPDATE1:
根据Darin Dimitrov请求添加我的ResetPasswordForUser视图:
@using TBS.Models
@model TBS.ViewModels.ResetPasswordForUserViewModel
@{
ViewBag.Title = "Reset Password";
}
@using …Run Code Online (Sandbox Code Playgroud) 在大量(每秒约50,000个请求)java web-app我正在使用ThreadLocal来执行应该按请求范围执行的任务.
我可以使用Spring请求范围实现相同的效果,我想知道哪个性能更好?
在代码中,使用ThreadLocal:
private static final ThreadLocal<SomeClass> myThreadLocal = new ThreadLocal<SomeClass>();
Run Code Online (Sandbox Code Playgroud)
并为每个http请求设置:
myThreadLocal.set(new SomeClass());
Run Code Online (Sandbox Code Playgroud)
使用Spring请求范围:
@Component
@Scope("request")
public class SomeClass{
...
}
Run Code Online (Sandbox Code Playgroud)
现在,会花多少钱:
myThreadLocal.get();
Run Code Online (Sandbox Code Playgroud)
要么
SpringContext.getBean(SomeClass.class);
Run Code Online (Sandbox Code Playgroud)
我想知道是否有人已经尝试过这样的基准测试?
我在Eclipse中有一个maven-spring项目,我在我的一个spring语境中有这个烦人的错误信息:
引用文件包含错误(jar:file:/M2_HOME/repository/org/springframework/spring-beans/3.1.2.RELEASE/spring-beans-3.1.2.RELEASE.jar!/ org/springframework/beans/factory/xml /spring-tool-3.1.xsd).有关更多信息,请右键单击"问题视图"中的消息,然后选择"显示详细信息..."
演出的秘密导致了这个:

我使用spring-data-jpa 1.2.0.RELEASE,其余的春季罐子是3.1.3.RELEASE.关于spring-data-commons-core - 我甚至没有依赖于我的pom中的这个jar,但我可以在我的m2存储库中看到它以及spring-data-commons-parent和版本1.4.0.RELEASE ,我不知道为什么(也许那些是spring-data-jpa的一部分?).
我的应用上下文架构:
<?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" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.2.xsd">
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我一直收到这个错误.基本上它没有任何影响,应用程序编译,部署和运行就好了,这只是Eclipse中令人讨厌的红色错误标记让我疯狂:)
刚开始使用Roboguice for android.尝试实现这个简单的上下文注入但获得此异常.我搜索了它并遇到了不少帖子,但没有解决我的问题.这是下面的例外,任何想法?
提前致谢
02-05 00:14:54.330: I/dalvikvm(777): Failed resolving Lcom/google/inject/Provider; interface 627 'Ljavax/inject/Provider;'
02-05 00:14:54.330: W/dalvikvm(777): Link of class 'Lcom/google/inject/Provider;' failed
02-05 00:14:54.340: I/dalvikvm(777): Failed resolving Lcom/google/inject/Provider; interface 627 'Ljavax/inject/Provider;'
02-05 00:14:54.340: W/dalvikvm(777): Link of class 'Lcom/google/inject/Provider;' failed
02-05 00:14:54.361: I/dalvikvm(777): Failed resolving Lcom/google/inject/Provider; interface 627 'Ljavax/inject/Provider;'
02-05 00:14:54.361: W/dalvikvm(777): Link of class 'Lcom/google/inject/Provider;' failed
02-05 00:14:54.361: W/dalvikvm(777): VFY: unable to find class referenced in signature (Lcom/google/inject/Provider;)
02-05 00:14:54.370: I/dalvikvm(777): Failed resolving Lcom/google/inject/Provider; interface 627 'Ljavax/inject/Provider;'
02-05 00:14:54.370: …Run Code Online (Sandbox Code Playgroud) 我的问题已在这个帖子中得到解决,但解释并不清楚.
我在我的一个pom.xml文件中有这个构建定义:
<build>
<finalName>${my.project}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
<extensions>
<extension>
<groupId>org.kuali.maven.wagons</groupId>
<artifactId>maven-s3-wagon</artifactId>
<version>1.1.19</version>
</extension>
</extensions>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/settings.properties</include>
</includes>
</resource>
</resources>
</build>
Run Code Online (Sandbox Code Playgroud)
请注意,我正在使用maven-s3-wagon扩展.接下来,我想有两个不同的配置文件,每个配置文件都有自己的设置,插件和扩展名,但maven不允许在配置文件下使用扩展标记.
当我尝试使用配置文件时:
<profiles>
<profile>
<id>local-build</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<finalName>${my.project}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
<extensions>
<extension>
<groupId>org.kuali.maven.wagons</groupId>
<artifactId>maven-s3-wagon</artifactId>
<version>1.1.19</version>
</extension>
</extensions>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/settings.properties</include>
</includes>
</resource>
</resources>
</build>
</profile>
</profiles>
Run Code Online (Sandbox Code Playgroud)
我的pom中出现错误:
cvc-complex-type.2.4.a: Invalid content was found …Run Code Online (Sandbox Code Playgroud) 我有一个高容量的Java应用程序,我必须将http帖子发送到另一台服务器.目前我正在使用org.apache.commons.httpclient库:
private static void sendData(String data) {
HttpClient httpclient = new HttpClient();
StringRequestEntity requestEntity;
try {
requestEntity = new StringRequestEntity(data, "application/json", "UTF-8");
String address = "http://<my host>/events/"
PostMethod postMethod = new PostMethod(address);
postMethod.setRequestEntity(requestEntity);
httpclient.executeMethod(postMethod);
} catch (Exception e) {
LOG.error("Failed to send data ", e);
}
}
Run Code Online (Sandbox Code Playgroud)
这意味着我正在同步发送我的http请求,这不适合我的多线程高容量应用程序.所以我想将这些调用更改为异步非阻塞http调用.
我经历了一些选项,如apache异步客户端和xsocket,但无法使其工作.
试图宁:
private static void sendEventToGrpahiteAsync(String event) {
LOG.info("\n" + "sendEventToGrpahiteAsync");
try (AsyncHttpClient asyncHttpClient = new AsyncHttpClient()) {
BoundRequestBuilder post = asyncHttpClient.preparePost();
post.addHeader("Content-Type", "application/json"); …Run Code Online (Sandbox Code Playgroud) 在大量实时Java Web应用程序中,我正在向apache kafka发送消息.目前我正在发送一个主题,但将来我可能需要向多个主题发送消息.
在这种情况下,我不确定每个主题创建一个制作人的天气,还是我应该使用单个制作人来处理我的所有主题?
这是我的代码:
props = new Properties();
props.put("zk.connect", <zk-ip1>:<2181>,<zk-ip3>:<2181>,<zk-ip3>:<2181>);
props.put("zk.connectiontimeout.ms", "1000000");
props.put("producer.type", "async");
Producer<String, Message> producer = new kafka.javaapi.producer.Producer<String, Message>(new ProducerConfig(props));
ProducerData<String, Message> producerData1 = new ProducerData<String, Message>("someTopic1", messageTosend);
ProducerData<String, Message> producerData2 = new ProducerData<String, Message>("someTopic2", messageTosend);
producer.send(producerData1);
producer.send(producerData2);
Run Code Online (Sandbox Code Playgroud)
如您所见,一旦创建了生产者,我就可以使用它将数据发送到不同的主题.我想知道什么是最佳做法?如果我的应用程序发送到多个主题(每个主题获得不同的数据)可以/我应该使用单个生产者还是应该创建多个生产者?什么时候(一般来说)我应该使用多个生产者?
java ×6
spring ×4
jpa ×2
android ×1
apache-kafka ×1
asp.net ×1
asynchronous ×1
cascade ×1
hibernate ×1
http-post ×1
many-to-one ×1
maven ×1
maven-3 ×1
mysql ×1
nonblocking ×1
one-to-many ×1
polymorphism ×1
roboguice ×1
spring-data ×1
thread-local ×1
xsd ×1