Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
a6341dc0
Commit
a6341dc0
authored
Jul 06, 2013
by
Phillip Webb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move @AssertMissingBean to tests
Use @AssertMissingBean only for tests.
parent
d91e8021
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
44 additions
and
15 deletions
+44
-15
Features.md
spring-zero-actuator/docs/Features.md
+0
-5
ServerPropertiesAutoConfigurationTests.java
...configure/web/ServerPropertiesAutoConfigurationTests.java
+42
-10
AssertMissingBean.java
...gframework/zero/context/annotation/AssertMissingBean.java
+1
-0
AssertMissingBeanCondition.java
...k/zero/context/annotation/AssertMissingBeanCondition.java
+1
-0
No files found.
spring-zero-actuator/docs/Features.md
View file @
a6341dc0
...
@@ -177,7 +177,6 @@ to change the default values imperatively in Java, so get more control
...
@@ -177,7 +177,6 @@ to change the default values imperatively in Java, so get more control
over the process. You can do this by declaring a bean of the same
over the process. You can do this by declaring a bean of the same
type in your application context, e.g. for the server properties:
type in your application context, e.g. for the server properties:
@AssertMissingBean(ServerProperties.class)
@Bean
@Bean
public ServerProperties serverProperties() {
public ServerProperties serverProperties() {
ServerProperties server = new ServerProperties();
ServerProperties server = new ServerProperties();
...
@@ -185,10 +184,6 @@ type in your application context, e.g. for the server properties:
...
@@ -185,10 +184,6 @@ type in your application context, e.g. for the server properties:
return server;
return server;
}
}
Note the use of
`@AssertMissingBean`
to guard against any mistakes
where the bean is already defined (and therefore might already have
been bound).
## Server Configuration
## Server Configuration
The
`ServerProperties`
are bound to application properties, and
The
`ServerProperties`
are bound to application properties, and
...
...
spring-zero-autoconfigure/src/test/java/org/springframework/zero/autoconfigure/web/ServerPropertiesConfigurationTests.java
→
spring-zero-autoconfigure/src/test/java/org/springframework/zero/autoconfigure/web/ServerProperties
Auto
ConfigurationTests.java
View file @
a6341dc0
...
@@ -18,15 +18,18 @@ package org.springframework.zero.autoconfigure.web;
...
@@ -18,15 +18,18 @@ package org.springframework.zero.autoconfigure.web;
import
java.io.File
;
import
java.io.File
;
import
org.hamcrest.Matchers
;
import
org.junit.After
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.junit.rules.ExpectedException
;
import
org.mockito.Mockito
;
import
org.mockito.Mockito
;
import
org.springframework.beans.factory.NoUniqueBeanDefinitionException
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.zero.TestUtils
;
import
org.springframework.zero.TestUtils
;
import
org.springframework.zero.autoconfigure.PropertyPlaceholderAutoConfiguration
;
import
org.springframework.zero.autoconfigure.PropertyPlaceholderAutoConfiguration
;
import
org.springframework.zero.autoconfigure.web.ServerPropertiesAutoConfiguration
;
import
org.springframework.zero.context.embedded.AnnotationConfigEmbeddedWebApplicationContext
;
import
org.springframework.zero.context.embedded.AnnotationConfigEmbeddedWebApplicationContext
;
import
org.springframework.zero.context.embedded.ConfigurableEmbeddedServletContainerFactory
;
import
org.springframework.zero.context.embedded.ConfigurableEmbeddedServletContainerFactory
;
import
org.springframework.zero.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor
;
import
org.springframework.zero.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor
;
...
@@ -42,16 +45,19 @@ import static org.junit.Assert.assertNotNull;
...
@@ -42,16 +45,19 @@ import static org.junit.Assert.assertNotNull;
*
*
* @author Dave Syer
* @author Dave Syer
*/
*/
public
class
ServerPropertiesConfigurationTests
{
public
class
ServerProperties
Auto
ConfigurationTests
{
private
static
ConfigurableEmbeddedServletContainerFactory
containerFactory
;
private
static
ConfigurableEmbeddedServletContainerFactory
containerFactory
;
@Rule
public
ExpectedException
thrown
=
ExpectedException
.
none
();
private
AnnotationConfigEmbeddedWebApplicationContext
context
;
private
AnnotationConfigEmbeddedWebApplicationContext
context
;
@Before
@Before
public
void
init
()
{
public
void
init
()
{
containerFactory
=
Mockito
containerFactory
=
.
mock
(
ConfigurableEmbeddedServletContainerFactory
.
class
);
Mockito
.
mock
(
ConfigurableEmbeddedServletContainerFactory
.
class
);
}
}
@After
@After
...
@@ -64,8 +70,8 @@ public class ServerPropertiesConfigurationTests {
...
@@ -64,8 +70,8 @@ public class ServerPropertiesConfigurationTests {
@Test
@Test
public
void
createFromConfigClass
()
throws
Exception
{
public
void
createFromConfigClass
()
throws
Exception
{
this
.
context
=
new
AnnotationConfigEmbeddedWebApplicationContext
();
this
.
context
=
new
AnnotationConfigEmbeddedWebApplicationContext
();
this
.
context
.
register
(
Config
.
class
,
ServerPropertiesAutoConfiguration
.
class
,
this
.
context
PropertyPlaceholderAutoConfiguration
.
class
);
.
register
(
Config
.
class
,
ServerPropertiesAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
TestUtils
.
addEnviroment
(
this
.
context
,
"server.port:9000"
);
TestUtils
.
addEnviroment
(
this
.
context
,
"server.port:9000"
);
this
.
context
.
refresh
();
this
.
context
.
refresh
();
ServerProperties
server
=
this
.
context
.
getBean
(
ServerProperties
.
class
);
ServerProperties
server
=
this
.
context
.
getBean
(
ServerProperties
.
class
);
...
@@ -78,8 +84,8 @@ public class ServerPropertiesConfigurationTests {
...
@@ -78,8 +84,8 @@ public class ServerPropertiesConfigurationTests {
public
void
tomcatProperties
()
throws
Exception
{
public
void
tomcatProperties
()
throws
Exception
{
containerFactory
=
Mockito
.
mock
(
TomcatEmbeddedServletContainerFactory
.
class
);
containerFactory
=
Mockito
.
mock
(
TomcatEmbeddedServletContainerFactory
.
class
);
this
.
context
=
new
AnnotationConfigEmbeddedWebApplicationContext
();
this
.
context
=
new
AnnotationConfigEmbeddedWebApplicationContext
();
this
.
context
.
register
(
Config
.
class
,
ServerPropertiesAutoConfiguration
.
class
,
this
.
context
PropertyPlaceholderAutoConfiguration
.
class
);
.
register
(
Config
.
class
,
ServerPropertiesAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
TestUtils
.
addEnviroment
(
this
.
context
,
"server.tomcat.basedir:target/foo"
);
TestUtils
.
addEnviroment
(
this
.
context
,
"server.tomcat.basedir:target/foo"
);
this
.
context
.
refresh
();
this
.
context
.
refresh
();
ServerProperties
server
=
this
.
context
.
getBean
(
ServerProperties
.
class
);
ServerProperties
server
=
this
.
context
.
getBean
(
ServerProperties
.
class
);
...
@@ -88,19 +94,45 @@ public class ServerPropertiesConfigurationTests {
...
@@ -88,19 +94,45 @@ public class ServerPropertiesConfigurationTests {
Mockito
.
verify
(
containerFactory
).
setPort
(
8080
);
Mockito
.
verify
(
containerFactory
).
setPort
(
8080
);
}
}
@Test
public
void
testAccidentalMultipleServerPropertiesBeans
()
throws
Exception
{
this
.
context
=
new
AnnotationConfigEmbeddedWebApplicationContext
();
this
.
context
.
register
(
Config
.
class
,
MutiServerPropertiesBeanConfig
.
class
,
ServerPropertiesAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
thrown
.
expectCause
(
Matchers
.<
Throwable
>
instanceOf
(
NoUniqueBeanDefinitionException
.
class
));
this
.
context
.
refresh
();
}
@Configuration
@Configuration
protected
static
class
Config
{
protected
static
class
Config
{
@Bean
@Bean
public
EmbeddedServletContainerFactory
containerFactory
()
{
public
EmbeddedServletContainerFactory
containerFactory
()
{
return
ServerPropertiesConfigurationTests
.
containerFactory
;
return
ServerProperties
Auto
ConfigurationTests
.
containerFactory
;
}
}
@Bean
@Bean
public
EmbeddedServletContainerCustomizerBeanPostProcessor
embeddedServletContainerCustomizerBeanPostProcessor
()
{
public
EmbeddedServletContainerCustomizerBeanPostProcessor
embeddedServletContainerCustomizerBeanPostProcessor
()
{
return
new
EmbeddedServletContainerCustomizerBeanPostProcessor
();
return
new
EmbeddedServletContainerCustomizerBeanPostProcessor
();
}
}
}
}
@Configuration
protected
static
class
MutiServerPropertiesBeanConfig
{
@Bean
public
ServerProperties
serverPropertiesOne
()
{
return
new
ServerProperties
();
}
@Bean
public
ServerProperties
serverPropertiesTwo
()
{
return
new
ServerProperties
();
}
}
}
}
spring-zero-core/src/
main
/java/org/springframework/zero/context/annotation/AssertMissingBean.java
→
spring-zero-core/src/
test
/java/org/springframework/zero/context/annotation/AssertMissingBean.java
View file @
a6341dc0
...
@@ -25,6 +25,7 @@ import java.lang.annotation.Target;
...
@@ -25,6 +25,7 @@ import java.lang.annotation.Target;
import
org.springframework.beans.factory.BeanFactory
;
import
org.springframework.beans.factory.BeanFactory
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.annotation.Conditional
;
import
org.springframework.context.annotation.Conditional
;
import
org.springframework.zero.context.annotation.ConditionalOnMissingBean
;
/**
/**
* {@link Conditional} that only matches when the specified bean classes and/or names are
* {@link Conditional} that only matches when the specified bean classes and/or names are
...
...
spring-zero-core/src/
main
/java/org/springframework/zero/context/annotation/AssertMissingBeanCondition.java
→
spring-zero-core/src/
test
/java/org/springframework/zero/context/annotation/AssertMissingBeanCondition.java
View file @
a6341dc0
...
@@ -22,6 +22,7 @@ import org.springframework.beans.factory.BeanCreationException;
...
@@ -22,6 +22,7 @@ import org.springframework.beans.factory.BeanCreationException;
import
org.springframework.context.annotation.Condition
;
import
org.springframework.context.annotation.Condition
;
import
org.springframework.context.annotation.ConditionContext
;
import
org.springframework.context.annotation.ConditionContext
;
import
org.springframework.core.type.AnnotatedTypeMetadata
;
import
org.springframework.core.type.AnnotatedTypeMetadata
;
import
org.springframework.zero.context.annotation.OnMissingBeanCondition
;
/**
/**
* {@link Condition} that checks that specific beans are missing.
* {@link Condition} that checks that specific beans are missing.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment