这是我的第一个Spring Boot代码.不幸的是,它总是关闭.我希望它能够连续运行,以便我的Web客户端可以从浏览器获取一些数据.
package hello;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
[@localhost initial]$ java -jar build/libs/gs-spring-boot-0.1.0.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ …Run Code Online (Sandbox Code Playgroud) 记录在这里它说
此特殊前缀指定必须获取与给定名称匹配的所有类路径资源(内部,这通常通过ClassLoader.getResources(...)调用),然后合并以形成最终的应用程序上下文定义.
有人可以解释一下吗?
使用classpath*:conf/appContext.xml而不是classpath:conf/appContext.xml没有星号有什么区别.
例
interface IA
{
public void someFunction();
}
@Resource(name="b")
class B implements IA
{
public void someFunction()
{
//busy code block
}
public void someBfunc()
{
//doing b things
}
}
@Resource(name="c")
class C implements IA
{
public void someFunction()
{
//busy code block
}
public void someCfunc()
{
//doing C things
}
}
class MyRunner
{
@Autowire
@Qualifier("b")
IA worker;
worker.someFunction();
}
Run Code Online (Sandbox Code Playgroud)
谁可以给我解释一下这个.
@Qualifier还是@Resource?我有一个控制器,提供对信息的RESTful访问:
@RequestMapping(method = RequestMethod.GET, value = Routes.BLAH_GET + "/{blahName}")
public ModelAndView getBlah(@PathVariable String blahName, HttpServletRequest request,
HttpServletResponse response) {
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,如果我使用带有特殊字符的路径变量命中服务器,它将被截断.例如: http:// localhost:8080/blah-server/blah/get/blah2010.08.19-02:25:47
参数blahName将是blah2010.08
但是,对request.getRequestURI()的调用包含传入的所有信息.
知道如何防止Spring截断@PathVariable吗?
有人可以解释Spring 3中的注释@RequestBody和@ResponseBody注释吗?它们适用于什么?任何例子都会很棒.
我有一个春季启动应用程序.
我的应用程序中有三个配置文件 - > 开发,登台和生产.所以我有3个文件
我的application.yml驻留在里面src/main/resources.我已将application.yml中的活动配置文件设置为:
spring:
profiles.active: development
Run Code Online (Sandbox Code Playgroud)
其他3个配置文件特定的配置文件存在于C:\config文件夹中.
我正在使用gradle插件进行eclipse.当我尝试执行" bootRun "时,我在eclipse中的gradle配置中设置命令行参数
-Dspring.profiles.active=staging -Dspring.config.location=C:\Config
Run Code Online (Sandbox Code Playgroud)
但是,命令行属性没有得到反映,我的活动配置文件总是被设置为开发(这是我在applications.yml文件中提到的那个).此外,不会在C:\ Config文件夹中搜索特定于配置文件的配置文件.
我想我在这里遗漏了一些东西.过去两天我一直试图解决这个问题.但没有运气.我真的很感激任何帮助.
我目前正在为一个项目创建一个REST-API,并且正在阅读关于最佳实践的文章.许多人似乎反对DTO,只是暴露域模型,而其他人似乎认为DTO(或用户模型或任何你想称之为的)是不好的做法.就个人而言,我认为这篇文章很有意义.
但是,我也理解DTO的缺点,包括所有额外的映射代码,可能与其DTO对应物100%相同的域模型等等.
我们的API主要是为了让其他客户端可以使用数据而创建的,但是如果我们做得对,我们也希望尽可能将它用于我们自己的Web GUI.
问题是我们可能不希望将所有域数据公开给其他客户端用户.大部分数据只在我们自己的Web应用程序中才有意义.此外,我们可能不希望在所有方案中公开有关对象的所有数据,尤其是与其他对象的关系等.例如,如果我们公开特定对象的列表,我们不一定要暴露整个对象层次结构; 这样对象的孩子就不会暴露,但可以通过链接(hateoas)发现.
我该如何解决这个问题?我正在考虑在我们的域模型上使用Jackson mixins来控制在给定不同场景的情况下会暴露哪些数据.或者我们应该一直使用DTO - 即使考虑到它的缺点和争议?
我目前正在评估基于Java的安全框架,我是一个Spring 3.0用户,所以SpringSecurity似乎是正确的选择,但Spring安全性似乎过于复杂,它似乎并不像是让安全性更容易实现, Shiro似乎更连贯,更容易理解.我正在寻找这两个框架之间的利弊列表.
我有一个Spring 2.5/Java/Tomcat应用程序.有以下bean,在许多地方的整个应用程序中使用
public class HibernateDeviceDao implements DeviceDao
Run Code Online (Sandbox Code Playgroud)
和以下新的bean:
public class JdbcDeviceDao implements DeviceDao
Run Code Online (Sandbox Code Playgroud)
第一个bean配置为(包括所有bean)
<context:component-scan base-package="com.initech.service.dao.hibernate" />
Run Code Online (Sandbox Code Playgroud)
第二个(新)bean是单独配置的
<bean id="jdbcDeviceDao" class="com.initech.service.dao.jdbc.JdbcDeviceDao">
<property name="dataSource" ref="jdbcDataSource">
</bean>
Run Code Online (Sandbox Code Playgroud)
当然,这会导致启动服务器时出现异常:
嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义类型为[com.sevenp.mobile.samplemgmt.service.dao.DeviceDao]的唯一bean:期望的单个匹配bean但找到2:[deviceDao,jdbcDeviceDao]
从一个试图像这样自动装豆的类
@Autowired
private DeviceDao hibernateDevicDao;
Run Code Online (Sandbox Code Playgroud)
因为有两个bean实现相同的接口.
是否可以配置bean以便这样做
1.我不必对已经HibernateDeviceDao自动装配的现有类进行更改
2.仍然可以像这样使用第二个(新)bean:
@Autowired
@Qualifier("jdbcDeviceDao")
Run Code Online (Sandbox Code Playgroud)
即我需要一种方法将HibernateDeviceDaobean 配置为要自动装配的默认bean,同时允许在使用注释JdbcDeviceDao显式指定时使用@Qualifier.
我试过设置属性
autowire-candidate="false"
Run Code Online (Sandbox Code Playgroud)
在JdbcDeviceDao的bean配置中:
<bean id="jdbcDeviceDao" class="com.initech.service.dao.jdbc.JdbcDeviceDao" autowire-candidate="false">
<property name="dataSource" ref="jdbcDataSource"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
因为Spring文档说明了这一点
指示在查找匹配候选项以满足另一个bean的自动装配要求时是否应考虑此Bean.请注意,这不会影响名称的显式引用,即使指定的bean未标记为autowire候选,也会解析引用.*
我解释为我仍然可以JdbcDeviceDao使用@Qualifier注释自动装配并具有HibernateDeviceDao默认bean.显然,我的解释是不正确的,因为这会在启动服务器时导致以下错误消息:
类型[class com.sevenp.mobile.samplemgmt.service.dao.jdbc.JdbcDeviceDao]的不满意依赖:预计至少有一个匹配的bean
来自我尝试使用限定符自动装配bean的类:
@Autowired
@Qualifier("jdbcDeviceDao")
Run Code Online (Sandbox Code Playgroud)
我有一个Person类:
@Entity
public class Person {
@Id
@GeneratedValue
private Long id;
@ManyToMany(fetch = FetchType.LAZY)
private List<Role> roles;
// etc
}
Run Code Online (Sandbox Code Playgroud)
与多对多的关系是懒惰的.
在我的控制器中我有
@Controller
@RequestMapping("/person")
public class PersonController {
@Autowired
PersonRepository personRepository;
@RequestMapping("/get")
public @ResponseBody Person getPerson() {
Person person = personRepository.findOne(1L);
return person;
}
}
Run Code Online (Sandbox Code Playgroud)
PersonRepository就是这个代码,根据本指南编写
public interface PersonRepository extends JpaRepository<Person, Long> {
}
Run Code Online (Sandbox Code Playgroud)
但是,在这个控制器中,我实际上需要惰性数据.如何触发加载?
试图访问它将失败
未能懒惰地初始化角色集合:no.dusken.momus.model.Person.roles,无法初始化代理 - 没有会话
或其他例外取决于我尝试的内容.
我的xml描述,如果需要的话.
谢谢.
java ×10
spring ×10
spring-mvc ×3
rest ×2
spring-boot ×2
annotations ×1
autowired ×1
classpath ×1
dto ×1
get ×1
gradle ×1
hateoas ×1
hibernate ×1
jpa ×1
shiro ×1
spring-data ×1
yaml ×1