> For the complete documentation index, see [llms.txt](https://jack80342.gitbook.io/spring-boot/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jack80342.gitbook.io/spring-boot/iv.-spring-boot-features/30.-working-with-nosql-technologies/30.3-neo4j/30.3.1-connecting-to-a-neo4j-database.md).

# 30.3.1 连接Neo4j数据库

你可以注入一个自动配置的`Neo4jSession`，`Session`，或`Neo4jOperations`实例，就像使用其他Spring Bean那样。该实例默认使用`localhost:7474`连接Neo4j服务器。下面的例子展示了怎么样注入一个Neo4j bean：

```java
@Component
public class MyBean {

    private final Neo4jTemplate neo4jTemplate;

    @Autowired
    public MyBean(Neo4jTemplate neo4jTemplate) {
        this.neo4jTemplate = neo4jTemplate;
    }

    // ...

}
```

添加自己的`org.neo4j.ogm.config.Configuration` `@Bean`，你就能完全控制该配置了。同时，添加一个`Neo4jOperations`类型的`@Bean`可以禁用自动配置。

通过`spring.data.neo4j.*`属性可以配置使用的用户和凭证，如下所示：

```
spring.data.neo4j.uri=http://my-server:7474
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=secret
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jack80342.gitbook.io/spring-boot/iv.-spring-boot-features/30.-working-with-nosql-technologies/30.3-neo4j/30.3.1-connecting-to-a-neo4j-database.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
