小编now*_*y94的帖子

@GetMapping和@RequestMapping(method = RequestMethod.GET)注释之间的区别

@GetMapping和之间有什么区别@RequestMapping(method = RequestMethod.GET)
我在一些Spring Reactive示例中看到过, @GetMapping而不是使用它@RequestMapping

java spring spring-mvc spring-4

130
推荐指数
4
解决办法
9万
查看次数

比较两个对象时,hashCode 扮演什么角色?

我决定学习一些 Java 的主要文档。我已经开始Object上课,我正在研究equals方法。我知道这equals是使用hashCode方法。虽然,hashCode返回 int 因此可以生成的唯一哈希码数量有限。

当我尝试比较两个相同的不同对象时会发生什么hashCode?这甚至可能吗?

java object collision

6
推荐指数
1
解决办法
4179
查看次数

无法配置Spring Data JPA:指定的类是一个接口

我已经开始使用Spring Data JPA了.不幸的是我无法配置它.我有Entity类,Repository接口,但是当我尝试测试它时,出了点问题.

源代码(我有setter/getter方法,但我跳过它以获得更好的代码):

@Entity 
public class Employee {

@Id
@GeneratedValue
private Long id;

private String firstName;
private String lastName;

public Employee() {
} 
}
Run Code Online (Sandbox Code Playgroud)

存储库类:

public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}
Run Code Online (Sandbox Code Playgroud)

组态:

@Configuration
@ComponentScan(basePackages = "com.szymon.repository")
@EnableJpaRepositories(basePackages = "com.szymon.repository")
public class AppConfig {

@Bean
public LocalEntityManagerFactoryBean entityManagerFactoryBean() {
    LocalEntityManagerFactoryBean localEntityManagerFactoryBean = new LocalEntityManagerFactoryBean();
    localEntityManagerFactoryBean.setPersistenceUnitName("postgres");
    return localEntityManagerFactoryBean;
}
}
Run Code Online (Sandbox Code Playgroud)

当我尝试测试它时:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = EmployeeRepository.class)
public class EmployeeRepositoryTest {

@Autowired
private EmployeeRepository employeeRepository;

@Test
public …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate jpa spring-data-jpa

6
推荐指数
1
解决办法
8203
查看次数

如果未找到,Spring Autowired null

@Autowired当没有定义应该自动装配的 bean 时,默认的Spring 实现会抛出错误。是否可以配置 Spring,将 null 分配给对象而不是抛出异常?

编辑:

我已经添加required=falseAutowired但它仍然无法正常工作。那是我的代码:

@Autowired
private ApplicationContext applicationContext;

@Autowired(required = false)
private HelloService helloService;

public HelloController() {
    message = "Hello World";
    System.out.println("Controller constructor");
}

@RequestMapping(method = RequestMethod.GET)
public ModelAndView helloWorld() {
    ModelAndView modelAndView = new ModelAndView("hello");
    if (helloService == null) {
        System.out.println(message);
    } else {
        helloService.hello();
        BeanDefinitionRegistry factory = (BeanDefinitionRegistry) applicationContext.getAutowireCapableBeanFactory();
        factory.removeBeanDefinition("helloService");
    }
    return modelAndView;
}
Run Code Online (Sandbox Code Playgroud)

在第一个请求中它是自动装配的,但是在删除 bean 后的下一个请求中factory.removeBeanDefinition("helloService"),再次构造控制器 bean,我得到NoSuchBeanDefinitionException

编辑2:

我创建了另一个具有以下主体的控制器:

@Autowired(required = false)
private TestService …
Run Code Online (Sandbox Code Playgroud)

java spring

6
推荐指数
2
解决办法
2879
查看次数

反应式 spring-data-solr 存储库

我找到了一篇关于 spring-data 反应式存储库的博客文章。有人提到,它目前支持 MongoDB、Apache Cassandra 和 Redis。

我们将在我们的反应式(基于 vert'x)架构中使用 spring-data-solr 存储库。

是否有可能在这种类型的存储库中轻松获得反应式支持?实施自己的反应式 solr 存储库需要多少努力?

java spring solr spring-data-solr project-reactor

5
推荐指数
1
解决办法
615
查看次数

创建自定义流畅的API

我将为我的应用程序创建自定义的流畅API.我决定检查现有库的代码,并在至少2个使用流畅api的项目中发现了一些怪物.

我看到有很多类和接口,只有通用类型的数量不同.

例如:在jOOQ库DerivedColumnList22中

在RxJava中:Action9

像这样的怪物的目的是什么?仅适用于性能案例吗?或者是流行api的一些共性和方式?

当你看到像这样的怪物时,用流畅的api实现自己的DSL看起来很可怕.

java dsl fluent

4
推荐指数
1
解决办法
500
查看次数

在 BeanPostProcessor 实现中获取 bean 的注释

我正在尝试实现我自己的 BeanPostProcessor 实现。

@Component
public class UserDetailsProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof User) {
            User user = (User) bean;
            List<Annotation> beanAnnotations = Arrays.asList(user.getClass().getAnnotations());
            for (Annotation annotation : beanAnnotations) {
                UserDetails userDetails = (UserDetails) annotation;
                System.out.println(userDetails.firstName());
                System.out.println(userDetails.lastName());
            }
        }

        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的 bean 定义:

@SpringBootApplication
public class HelloReactiveApplication {

    public static void main(String[] args) { …
Run Code Online (Sandbox Code Playgroud)

java spring annotations spring-boot

3
推荐指数
1
解决办法
3620
查看次数

来自 ModelAndView 的视图与来自 ViewResolver 的视图

在 Spring MVC 中,当请求到来时,DispatcherServlet指定HandlerMapping指示哪个控制器应该处理请求。Controller处理请求并返回指定的模型View

我不明白在那之后会发生什么,什么时候ViewResolver开始他的工作。如果View已经由 指定Controller,为什么还有一个ViewResolver层?它实际上有什么作用?不应该ViewController?之前指定或与之平行?或者Controller应该触发ViewResolver层?

java spring spring-mvc web

2
推荐指数
1
解决办法
717
查看次数

引导程序中的方柱

我正在尝试创建一个正方形网格以提供一些导航功能。

我有它与js一起使用,但我不喜欢这种解决方案。我正在使用Bootstrap 3

<div class="container">
<div class="row">
    <div class="col-xs-4" style="background-color: lightgray"></div>
    <div class="col-xs-4" style="background-color: lightgreen"></div>
    <div class="col-xs-4" style="background-color: lightgray"></div>
    <div class="col-xs-4" style="background-color: lightgreen"></div>
    <div class="col-xs-4" style="background-color: lightgray"></div>
    <div class="col-xs-4" style="background-color: lightgreen"></div>
</div>
</div>

var divs = $(".row > div");
var width = divs.width();
divs.height(width)
Run Code Online (Sandbox Code Playgroud)

如何仅使用CSS实现它?

jsfiddle

html javascript css jquery twitter-bootstrap

2
推荐指数
1
解决办法
4841
查看次数

如何使用触发器 Postgres 打印新值

我想为插入操作创建触发器,以及打印插入值的过程。

CREATE TRIGGER added_product_info_trigger
BEFORE INSERT
ON products
EXECUTE PROCEDURE added_product_info();
Run Code Online (Sandbox Code Playgroud)

还有我的程序

CREATE OR REPLACE FUNCTION added_product_info()
RETURNS trigger
AS
$$
    (Select p.productname, s.companyname from products as p, suppliers as s
    where p.supplierid = s.supplierid)
$$ LANGUAGE SQL;
Run Code Online (Sandbox Code Playgroud)

如何打印我插入的值?

sql postgresql triggers plpgsql

1
推荐指数
1
解决办法
6042
查看次数