Android提供旋转矢量传感器和方向传感器.我知道它们会返回不同的数据,因为对于矢量传感器,我们有角度,在方向传感器中我们有角度.但是概念上的区别是什么?我从文档中无法理解.哪一个提供了三维空间中设备的方向?我糊涂了!
我有一个使用Hibernate 4和Spring Transactions的Spring 3.2应用程序.所有方法都运行良好,我可以正确访问数据库以保存或检索实体.然后,我介绍了一些多线程,并且由于每个线程都访问了db,我从Hibernate收到以下错误:
org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
Run Code Online (Sandbox Code Playgroud)
我从网上读到我要添加<prop key="hibernate.current_session_context_class">thread</prop>到我的Hibernate配置中,但是现在每次我尝试访问db时都会得到:
org.hibernate.HibernateException: saveOrUpdate is not valid without active transaction
Run Code Online (Sandbox Code Playgroud)
但是我的服务方法是注释的@Transactional,并且在添加之前一切正常<prop key="hibernate.current_session_context_class">thread</prop>.
为什么没有交易,尽管方法是用@Transactional注释的?我怎么解决这个问题?
这是我的Hibernate配置(包括会话上下文属性):
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- Hibernate session factory -->
<bean
id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name="dataSource" >
<ref bean="dataSource" />
</property>
<property name="hibernateProperties" >
<props>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
<property name="annotatedClasses" …Run Code Online (Sandbox Code Playgroud) 在我的Spring MVC服务器中,我希望收到包含文件(图像)和一些JSON元数据的multipart/form-data请求.我可以在JSON部分中构建格式良好的多部分请求Content-Type=application/json.Spring服务的形式如下:
@RequestMapping(value = MY_URL, method=RequestMethod.POST, headers="Content-Type=multipart/form-data")
public void myMethod(@RequestParam("image") MultipartFile file, @RequestParam("json") MyClass myClass) {
...
}
Run Code Online (Sandbox Code Playgroud)
该文件已正确上传,但我遇到了JSON部分的问题.我收到此错误:
org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'myPackage.MyClass'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [myPackage.MyClass]: no matching editors or conversion strategy found
Run Code Online (Sandbox Code Playgroud)
如果我不使用多部分请求JSON转换使用Jackson 2很好,但是当使用多部分我得到之前的错误.我想我必须配置多部分消息转换器以支持JSON作为消息的一部分,但我不知道如何.这是我的配置:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
Run Code Online (Sandbox Code Playgroud)
如果我使用String作为myClass的类型而不是MyClass,一切都很好,但我想使用Spring MVC支持参数转换.
以RESTful方式思考,使用POST在单个调用中创建资源及其子资源是否正确?在我的应用程序中,我有资源/notices/{notice}和子资源/notices/{notice}/photos/{photo}.一个{photo}不能存在没有{notice},但{notice}没有必然的照片.通常,我必须先做一个POST来创建一个通知,然后另一个POST来添加一张照片.
现在我想允许创建一个直接附加照片的通知,通过单个POST请求创建/notices/{notice}和/notices/{notice}/photos/{photo}通知/ {notice}/photos/{photo},其中包含描述两种资源的多部分内容(JSON)通知,照片的二进制).我想我只会为子资源返回Location标头.
从本质上讲,我希望这可以防止Android客户端向服务器发送两个POST请求以上传带有照片的通知.它是否正确?或者它是否违反了REST原则?我应该考虑将它们分开并提出两个不同的要求吗?或者将照片视为与通知单独的实体是错误的吗?我应该只保留/notices/{notice}资源,使用PUT添加照片吗?
哪个是最好的解决方案?
我正在使用Spring MVC来公开RESTful服务.我已经通过HTTPBasicAuthentication启用了身份验证,并且使用<security:http>i可以控制哪些角色可以访问URL.
现在我想使用@Secured注释.我试图将它添加到Controller方法但它不起作用.它什么都不做.
这是我的Controller班级:
@Controller
@RequestMapping("/*")
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
private static final String USERS = "/users";
private static final String USER = USERS+"/{userId:.*}";
@RequestMapping(value=USER, method=RequestMethod.GET)
@Secured(value = {"ROLE_ADMIN"})
public @ResponseBody User signin(@PathVariable String userId) {
logger.info("GET users/"+userId+" received");
User user= service.getUser(userId);
if(user==null)
throw new ResourceNotFoundException();
return user;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的security-context.xml:
<http auto-config='true'>
<intercept-url pattern="/**" access="ROLE_USER"/>
</http>
<global-method-security secured-annotations="enabled" />
<authentication-manager>
<authentication-provider>
<user-service>
<user name="admin@somedomain.com" …Run Code Online (Sandbox Code Playgroud) 我在尝试为Spring应用程序构建测试套件时遇到了问题.我是Maven的新手,我找不到有什么问题.我已经添加到我的pom.xml中了
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.0.0.RELEASE</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
但我仍然从Eclipse得到错误:The import org.springframework.test cannot be resolved.
在讨论之后(http://appfuse.547863.n4.nabble.com/spring-test-package-not-found-td1596479.html),我试图添加<scope>provided<scope>但没有成功.
这是我的完整pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.mose</groupId>
<artifactId>emergency-alert-server</artifactId>
<name>emergency-alert-server</name>
<version>1.0.0-BUILD-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<java-version>1.6</java-version>
<org.springframework-version>3.0.5.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.1</org.slf4j-version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- AspectJ --> …Run Code Online (Sandbox Code Playgroud) 我必须设计像过滤器这样的实体,由Filter接口表示,声明apply(Content content)可以应用于内容对象的方法。过滤器可以以链的形式组合在一起,类似于工作流程,但它们是动态的。例如,如果FilterA返回X,那么我将应用filterB,而接收结果Y将导致应用FilterC。过滤器链是特定于应用程序的,我还没有决定如何允许构建过滤器链。
我会以与某些工作流框架相同的方式设计此行为:管理器组件迭代过滤器列表,并调用filter.apply(content)每个过滤器。但如何允许像 if/else 语句这样的动态性呢?
现在我构思了一个 Workflow 或 FilterChain 接口,声明了一个getNextFilter(previousResult). 实现此接口可以声明特定于应用程序的工作流程。但是 Workflow 接口的实现会很无聊:跟踪当前步骤(整数?),然后在每次getNextFilter()调用时,通过 switch/case 语句确定下一个过滤器?!?
哪种解决方案可能更好?如何声明链?
我使用的是Java和Spring,所以可以使用IoC。
我正在尝试编写一个在后台运行的简单Android服务,并从LocationClient(Google Map API Android V2)接收位置更新.问题是,当屏幕关闭时,我的服务不再接收位置更新.我试图检查服务是否处于活动状态,即使关闭屏幕也是如此(它有一个TimerTask调度日志).当屏幕打开时,我可以接收位置更新,但是当屏幕关闭时,我只看到TimerTask的日志,我没有收到任何位置更新.唤醒屏幕再次打开位置更新.怎么解决这个问题?
这是我的简单服务:
public class LocationService extends Service implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationListener{
private static final String TAG = LocationService.class.getSimpleName();
private LocationClient mLocationClient;
private Timer timer;
private static final LocationRequest REQUEST = LocationRequest.create()
.setInterval(5*1000) // 5 seconds
.setFastestInterval(3*1000) // 3 seconds
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
@Override
public void onCreate() {
Log.d(TAG, "Creating..");
mLocationClient = new LocationClient(this, this, this);
timer = new Timer();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Starting..");
if(!mLocationClient.isConnected() || !mLocationClient.isConnecting()) {
Log.d(TAG, "Connecting …Run Code Online (Sandbox Code Playgroud) 我有一些 RESTful 服务,用 Spring MVC 实现,公开了一组资源。我已经使用基于 HTTPBasicAuthentication 和 HTTPS 的身份验证。某些资源必须仅供某些用户访问。
例如,我希望 URI/users/{userid}/photos中的所有子资源只能由用户访问userid。实际上在我的应用程序中,所有经过身份验证的用户都可以访问它们。我怎样才能保护他们免受其他用户的侵害userid?如果我只想允许一部分用户(例如userid的朋友)访问此资源,该怎么办?
我想在TextView的右侧放置一个TextView,在TextView的右侧放置一个控件(例如,一个CheckBox).我希望控件在屏幕上左对齐.这并不难通过LinearLayout或RelativeLayout获得.例如,我使用LinearLayout执行此操作:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/todo_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Small"
android:maxLines="2"
android:textStyle="bold" />
<View
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<CheckBox
android:id="@+id/todo_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:focusable="false"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
问题是,当TextView的文本太长时,它会将复选框推出屏幕,并且复选框不再可见.相反,我希望复选框固定在屏幕的右端,如果需要,TextView最终会分成两行.我怎样才能做到这一点?
在我的Android应用程序中,我必须使用加速计和其他传感器.由于此传感器未由虚拟设备模拟,因此我将使用SensorSimulator项目.问题是SensorSimulator的API看起来与Android的不同.因此,如果我在真实或虚拟设备中,我将使用不同的代码.它是否存在以编程方式检测它的方法?或者您知道其他解决方案吗?
spring ×5
android ×4
java ×3
spring-mvc ×3
rest ×2
hibernate ×1
jackson ×1
maven ×1
oop ×1
restful-url ×1
spring-test ×1